asp.net-mvc-3 - MVC 3 模型属性未在由 html.action 调用的部分 View 中设置

标签 asp.net-mvc-3 data-binding model partial-views

我的应用程序有问题,请尝试通过示例进行解释,我有一个表单,该表单包含多个文本框和下拉列表。为了可重用性,我将 3 个下拉列表合并到一个局部 View 中,这个局部 View 我用@Html.Action 加载,这工作正常,当我启动表单时,我看到一切都显示为它应该的样子,虽然我不知道为什么,但那些是必需的下拉列表直接以红色开头显示并表示它是必填字段。

但是,当我填写所有内容,并从下拉列表中选择值,然后单击“确定”时,下拉列表中的值为 NULL。

我认为通过代码示例会更容易理解:

主要型号:

public class FormModel
{
    [Required]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Birthdate")]
    public DateTime? Birthdate { get; set; }
    //This is what i'm talking about, that is not being set, the location
    public Location Location { get; set; } 
}

这是位置类,然后将其传递给局部 View ,通常我认为应该设置它,它看起来像这样:
public class Location
{
    [Required]
    [Display(Name = "Country")]
    public string CountryId { get; set; }

    [Required]
    [Display(Name = "Region")]
    public string RegionId { get; set; }

    [Required]
    [Display(Name = "City")]
    public string CityId { get; set; }
  }

现在我们有该位置的局部 View :
@model TestWebsite.Models.Location
<tr>
    <td class="editor-label">
        @Html.LabelFor(m => m.CountryId)
    </td>
    <td class="editor-field">
        @Html.DropDownListFor(m => m.CountryId, Model.Countries, "---select--", null)
    </td>
    <td>
        @Html.ValidationMessageFor(m => m.CountryId)
    </td>
</tr>
<!-- Region -->
<tr>
    <td class="editor-label">
        @Html.LabelFor(m => m.RegionId)
    </td>
    <td class="editor-field">
        @Html.DropDownListFor(m => m.RegionId, Enumerable.Empty<SelectListItem>(), "---select--", null)
    </td>
    <td>
        @Html.ValidationMessageFor(m => m.RegionId)
    </td>
</tr>
<!-- City -->
<tr>
    <td class="editor-label">
        @Html.LabelFor(m => m.CityId)
    </td>
    <td class="editor-field">
        @Html.DropDownListFor(m => m.CityId, Enumerable.Empty<SelectListItem>(), "---select--", null)
    </td>
    <td>
        @Html.ValidationMessageFor(m => m.CityId)
    </td>
</tr>

然后我们以这样的形式调用这个局部 View :
@Html.Action("LocationGroup", "Account", Model.Location)

最后在 Controller 中:
    [ChildActionOnly]
    public ActionResult LocationGroup(Location model)
    {
        model.Countries = GetCountries();
        return PartialView("_LocationView", model);
    }

我知道这是很多代码,但我希望你能帮助我......

最佳答案

我认为您应该使用编辑器模板:

@model TestWebsite.Models.FormModel
@using(Html.BeginForm())
{
    @Html.EditorFor(x => x.Location)
    <input type="submit" value="save"/>
}

并添加部分内部 ~/Views/[ControllerName]/EditorTemplates/Location.cshtml~/Views/Shared/EditorTemplates/Location.cshtml
模板代码:
@model TestWebsite.Models.Location
<tr>
    <td class="editor-label">
        @Html.LabelFor(m => m.CountryId)
    </td>
    <td class="editor-field">
        @Html.DropDownListFor(m => m.CountryId, Model.Countries, "---select--", null)
    </td>
    <td>
        @Html.ValidationMessageFor(m => m.CountryId)
    </td>
</tr>
<!-- Region -->
<tr>
    <td class="editor-label">
        @Html.LabelFor(m => m.RegionId)
    </td>
    <td class="editor-field">
        @Html.DropDownListFor(m => m.RegionId, Enumerable.Empty<SelectListItem>(), "---select--", null)
    </td>
    <td>
        @Html.ValidationMessageFor(m => m.RegionId)
    </td>
</tr>
<!-- City -->
<tr>
    <td class="editor-label">
        @Html.LabelFor(m => m.CityId)
    </td>
    <td class="editor-field">
        @Html.DropDownListFor(m => m.CityId, Enumerable.Empty<SelectListItem>(), "---select--", null)
    </td>
    <td>
        @Html.ValidationMessageFor(m => m.CityId)
    </td>
</tr>

但是,如果您更喜欢在某个位置使用部分(不遵循约定),您可以指定位置:
@Html.EditorFor(x => x.Location, "~/Views/SpecifyLocation/_Location.cshtml")

关于asp.net-mvc-3 - MVC 3 模型属性未在由 html.action 调用的部分 View 中设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752771/

相关文章:

python - 如何继承ForeignKey扩展模型字段?

asp.net-mvc-3 - 基于简单数学的验证码

silverlight - 在 Silverlight 中从 View 模型绑定(bind)到子用户控件的 View 模型? 2 个来源 - 1 个目标

javascript - 类型错误 : "ctx.cart is undefined"

python - Django:如何使用自定义模板标签动态获取字段值?

ruby-on-rails - Rails Simple Form自定义关联选择字段

c# - 我应该将用户配置文件信息存储在缓存还是 session 中?

asp.net - 如何创建模型绑定(bind)的“是/否”单选按钮,该按钮在 ASP.NET MVC 中的页面加载时未选择任何选项?

asp.net-mvc-3 - MVC 3 Razor 使用 actionlink 将 webgrid 行数据传递到 Controller

javascript - 如何将可见绑定(bind)与单选按钮可观察参数一起使用?