Aln*_*dru 12 data-binding model partial-views asp.net-mvc-3
我的应用程序有问题,生病尝试通过示例解释,我有一个表单,这个表单存在几个文本框和下拉列表.为了可重用性,我将3个下拉列表合并到一个局部视图中,这个部分视图我加载了@ 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; }
}
Run Code Online (Sandbox Code Playgroud)
这是位置类,然后传递给局部视图,通常我认为应该设置,它看起来像这样:
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; }
}
Run Code Online (Sandbox Code Playgroud)
现在我们有一个部分位置视图:
@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>
Run Code Online (Sandbox Code Playgroud)
然后我们在这样的形式中调用这个局部视图:
@Html.Action("LocationGroup", "Account", Model.Location)
Run Code Online (Sandbox Code Playgroud)
最后在控制器中:
[ChildActionOnly]
public ActionResult LocationGroup(Location model)
{
model.Countries = GetCountries();
return PartialView("_LocationView", model);
}
Run Code Online (Sandbox Code Playgroud)
我知道这是很多代码,但我希望你能帮助我......
我认为你应该使用编辑器模板:
@model TestWebsite.Models.FormModel
@using(Html.BeginForm())
{
@Html.EditorFor(x => x.Location)
<input type="submit" value="save"/>
}
Run Code Online (Sandbox Code Playgroud)
并添加部分内部~/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>
Run Code Online (Sandbox Code Playgroud)
但是,如果您希望在某个位置部分显示(不遵循约定),您可以指定该位置:
@Html.EditorFor(x => x.Location, "~/Views/SpecifyLocation/_Location.cshtml")
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1254 次 |
最近记录: |