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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – MVC 3模型属性未在html.action调用的部分视图中设置前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的应用程序有一个问题,病例尝试通过示例说明,我有一个表单,这种形式存在几个文本框和下拉列表.为了可重用性,我将3个下拉列表合并到局部视图中,这个部分视图我用@ Html.Action加载,这可以正常工作,当我启动表单时,我看到一切似乎应该是,尽管我不知道为什么,但是那些需要下拉列表直接显示为红色开始,表示为必填字段.

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

我认为一个代码示例会更容易理解:

主要型号:

  1. public class FormModel
  2. {
  3. [required]
  4. public string UserName { get; set; }
  5.  
  6. [required]
  7. [Display(Name = "Birthdate")]
  8. public DateTime? Birthdate { get; set; }
  9. //This is what i'm talking about,that is not being set,the location
  10. public Location Location { get; set; }
  11. }

这是位置类,然后传递给局部视图,通常我认为应该设置,它看起来像这样:

  1. public class Location
  2. {
  3. [required]
  4. [Display(Name = "Country")]
  5. public string CountryId { get; set; }
  6.  
  7. [required]
  8. [Display(Name = "Region")]
  9. public string RegionId { get; set; }
  10.  
  11. [required]
  12. [Display(Name = "City")]
  13. public string CityId { get; set; }
  14. }

现在我们有一个局部视图的位置:

  1. @model TestWebsite.Models.Location
  2. <tr>
  3. <td class="editor-label">
  4. @Html.LabelFor(m => m.CountryId)
  5. </td>
  6. <td class="editor-field">
  7. @Html.DropDownListFor(m => m.CountryId,Model.Countries,"---select--",null)
  8. </td>
  9. <td>
  10. @Html.ValidationMessageFor(m => m.CountryId)
  11. </td>
  12. </tr>
  13. <!-- Region -->
  14. <tr>
  15. <td class="editor-label">
  16. @Html.LabelFor(m => m.RegionId)
  17. </td>
  18. <td class="editor-field">
  19. @Html.DropDownListFor(m => m.RegionId,Enumerable.Empty<SelectListItem>(),null)
  20. </td>
  21. <td>
  22. @Html.ValidationMessageFor(m => m.RegionId)
  23. </td>
  24. </tr>
  25. <!-- City -->
  26. <tr>
  27. <td class="editor-label">
  28. @Html.LabelFor(m => m.CityId)
  29. </td>
  30. <td class="editor-field">
  31. @Html.DropDownListFor(m => m.CityId,null)
  32. </td>
  33. <td>
  34. @Html.ValidationMessageFor(m => m.CityId)
  35. </td>
  36. </tr>

那么我们将这个局部视图称为这样的形式:

  1. @Html.Action("LocationGroup","Account",Model.Location)

最后在控制器:

  1. [ChildActionOnly]
  2. public ActionResult LocationGroup(Location model)
  3. {
  4. model.Countries = GetCountries();
  5. return PartialView("_LocationView",model);
  6. }

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

解决方法

我想你应该使用编辑器模板:
  1. @model TestWebsite.Models.FormModel
  2. @using(Html.BeginForm())
  3. {
  4. @Html.EditorFor(x => x.Location)
  5. <input type="submit" value="save"/>
  6. }

添加部分内部〜/ Views / [ControllerName] /EditorTemplates/Location.cshtml或〜/ Views / Shared / EditorTemplates / Location.cshtml

模板代码

  1. @model TestWebsite.Models.Location
  2. <tr>
  3. <td class="editor-label">
  4. @Html.LabelFor(m => m.CountryId)
  5. </td>
  6. <td class="editor-field">
  7. @Html.DropDownListFor(m => m.CountryId,null)
  8. </td>
  9. <td>
  10. @Html.ValidationMessageFor(m => m.CityId)
  11. </td>
  12. </tr>

但是,如果您希望在某些位置部分(不遵循约定),您可以指定位置:

  1. @Html.EditorFor(x => x.Location,"~/Views/SpecifyLocation/_Location.cshtml")

猜你在找的asp.Net相关文章