我是一个沉默的读者堆叠溢出,几乎在所有情况下,我找到了解决我的问题从其他人的帖子,直到昨天,当我被困在一些东西,无法找到解决办法。
下面是对我的应用程序体系结构的一些解释:
我有一个独立的c#项目中的代码优先实体模型,我将它引用到我的网络项目。我有以下实体:
public class Employee
{
public Employee();
public int EmployeeId {get;set;}
public DateTime? DegreeCompleted{ get; set; }
public virtual University University { get; set; }
}
public class University
{
public University();
public int UniversityId {get;set;}
public short? TotalDegrees{ get; set; }
public short? TempTotalDegrees{ get; set; }
}在视图端,我有一个带有Index.cshtml的Home视图,它将Views/Shared/EditorTemplates/University.cshtml引用如下:
@using (Ajax.BeginForm("SaveEmployee", new AjaxOptions
{
HttpMethod="POST",
OnBegin="disableSubmit",
OnComplete = "enableSubmit",
OnFailure = "enableSubmit",
InsertionMode=InsertionMode.Replace
}))
{
@Html.HiddenFor(model=>model.EmployeeId)
@Html.EditorFor(m => m.University, new { EmployeeId = Model.EmployeeId})以下是University.cshtml的外观:
@Html.HiddenFor(model=>model.UniversityId)
@Html.TextBoxFor(x => Model.TotalDegrees, new { @class="form-control", @min="5",@max="100",@type="number",@value=(Model.TotalDegrees.HasValue ? Model.TotalDegrees.Value : 5)})在Index.cshtml上,当我单击submit按钮时,它会将Employee对象发回服务器,但是
Employee.University.TotalDegrees是空的,即使用户填写了值。
如果我将@Html.HiddenFor(model=>model.UniversityId)从University.cshtml中删除,则Employee.University在post上为null。
我试过使用@Html.EditorFor(model=>model.TotalDegrees)和@Html.TextBoxFor(model=>model.TotalDegrees),但它们似乎都没有用。
如果我把所有的东西从University.cshtml转移到主视图,一切看起来都很好。
任何帮助或建议都很感激。
发布于 2015-08-17 02:35:02
妈的!花了16个小时来确认设计师给我的这个
<form role='form'>
<div class="form-group">
<input />
</div>
</form>因此,除了我自己的表单之外,还有一个表单与EditorTemplate中的每个字段相关联,它将表单张贴到没有位置:(
谢谢斯蒂芬的帮助,很抱歉浪费了时间:)
发布于 2015-08-14 05:25:45
您的问题是您写错了lambda x => Model.TotalDegrees。只需解决这个问题:
@Html.TextBoxFor(x => x.TotalDegrees, new { @class="form-control", @min="5",@max="100",@type="number",@value=(Model.TotalDegrees.HasValue ? Model.TotalDegrees.Value : 5)})https://stackoverflow.com/questions/32002648
复制相似问题