asp.net-mvc-3 – MVC 3模型属性未在html.action调用的部分视图中设置
|
我的应用程序有一个问题,病例尝试通过示例说明,我有一个表单,这种形式存在几个文本框和下拉列表.为了可重用性,我将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; }
}
这是位置类,然后传递给局部视图,通常我认为应该设置,它看起来像这样: 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; }
}
现在我们有一个局部视图的位置: @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>(),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,null)
</td>
<td>
@Html.ValidationMessageFor(m => m.CityId)
</td>
</tr>
那么我们将这个局部视图称为这样的形式: @Html.Action("LocationGroup","Account",Model.Location)
最后在控制器: [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,null)
</td>
<td>
@Html.ValidationMessageFor(m => m.CityId)
</td>
</tr>
但是,如果您希望在某些位置部分(不遵循约定),您可以指定位置: @Html.EditorFor(x => x.Location,"~/Views/SpecifyLocation/_Location.cshtml") (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 从通用列表中将ListItem添加到DropDownList
- asp.net – RedirectToAction MVC2的问题 – 不能将类型“S
- 在ASP.NET Web服务(ASMX)的JSON请求中,什么时候区分大小写是
- 如何从asp.net中的客户端网络摄像头捕获图像
- asp.net – 我应该继续研究MVC 1.0上的项目还是停止学习MVC
- asp.net – 如何确定哪个按钮导致回发
- asp.net使用多线程更新UI
- asp.net-mvc – 使用RadioButtons的可空值布尔的MVC3 Edito
- asp.net-mvc-3 – 在MVC3中使用Page.User.Identity.Name
- asp.net-mvc – 在DropDownList ASP.NET MVC中获取所选项目
- asp.net Web.config 详细配置说明
- asp.net-mvc – 在ASP.net MVC 2.0中使用Url.Con
- asp.net-core – 如何在IIS上运行ASP.NET MVC6而
- asp.net – 如何显示UTC时间作为本地时间在网页?
- asp.net-mvc – Html.BeginForm的默认区域是否始
- asp.net-mvc – 变化:*错误地添加到http标头响应
- asp.net-mvc – 如何创建安装文件来安装MVC .net
- asp-classic – 如何判断ASP中的变量是否已声明
- asp-classic – ASP/VBScript – Int()vs CInt()
- asp.net – 奇数编号单元格未添加到Pdf
