我是ASP mvc和实体框架的新手 我有一个由实体框架创建的模型
serialver
如您所见,public partial class Privilege
{
public Privilege()
{
this.Role_Privilege_Map = new HashSet<Role_Privilege_Map>();
}
public int PrivilegeId { get; set; }
[Required(ErrorMessage="*")]
[Display(Name = "Privilege Name")]
public string PrivilegeName { get; set; }
[Required(ErrorMessage = "*")]
public Nullable<int> ModuleId { get; set; }
public virtual module module { get; set; }
public virtual ICollection<Role_Privilege_Map> Role_Privilege_Map { get; set; }
}
是一个导航属性。
我已将此模型绑定到视图
module
我有一个更新按钮,用于更新此模型,说明权限名称。现在我的行动
@for (var i = 0; i < Model.Count(); i++)
{
<tr>
<td>
@Html.DisplayFor(modelItem => modelItem[i].PrivilegeName)
@Html.HiddenFor(modelItem => modelItem[i].PrivilegeId)
@Html.HiddenFor(modelItem => modelItem[i].PrivilegeName)
</td>
<td>
@Html.DisplayFor(modelItem => modelItem[i].module.ModuleName)
@Html.HiddenFor(modelItem => modelItem[i].ModuleId)
@Html.HiddenFor(modelItem => modelItem[i].module.ModuleName)
@Html.HiddenFor(modelItem => modelItem[i].module)
</td>
<td>
@Html.CheckBoxFor(modelItem => modelItem[i].Checked)
@Html.HiddenFor(modelItem => modelItem[i].Checked)
</td>
<td>
@Html.ActionLink("Edit", "OpenEditPrivilegeDialog", "RolePrivilegeMapping",
new { id = Model[i].PrivilegeId },
new { @class = "actionHyperLink edit_Privilege_Link" }) |
@Html.ActionLink("Delete", "DeletePrivilege","RolePrivilegeMapping",
new { id = Model[i].PrivilegeId },
new { @class = "actionHyperLink Delete_Privilege_Link" })
</td>
</tr>
}
返回false。我放了一个断点,并且知道导航属性为null,这可能是模型无效的原因。我怎么能超越这个。正如您在代码中看到的,我添加了一个用于绑定导航属性的隐藏字段
为什么会这样?任何帮助将不胜感激
根据wiz kid提供的帖子ModelState.IsValid == false, why?
我知道那里出现了褐色异常
public ActionResult UpdateRolePrivilege(IList<One_Track.Models.Privilege> updatedPrivilege)
{
if (ModelState.IsValid)
{
}
else
{
ViewBag.PrivilegeMessage = "Privileges updation Failed.";
}
}
return PartialView("PrivilegePartial", sample.GetPrivilegeNames());
}
所以我从视图中删除了该行
The parameter conversion from type 'System.String' to type 'sample.Models.module' failed because no type converter can convert between these types.
at System.Web.Mvc.ValueProviderResult.ConvertSimpleType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.UnwrapPossibleArrayType(CultureInfo culture, Object value, Type destinationType)
at System.Web.Mvc.ValueProviderResult.ConvertTo(Type type, CultureInfo culture)
at System.Web.Mvc.DefaultModelBinder.ConvertProviderResult(ModelStateDictionary modelState, String modelStateKey, ValueProviderResult valueProviderResult, Type destinationType)}
这解决了问题