0

I am new to ASP mvc and entity framework
I have a model created by entity framework as

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; }
    }

As you can see module is a navigational property.

I have binded this model to a view as

@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>
        }

I have an Update button for updating this model say Privilege name. Now in my action

 public ActionResult UpdateRolePrivilege(IList<One_Track.Models.Privilege> updatedPrivilege)
        {


                if (ModelState.IsValid)
                {

                }
                else
                {
                    ViewBag.PrivilegeMessage = "Privileges updation Failed.";
                }
            }

            return PartialView("PrivilegePartial", sample.GetPrivilegeNames());
        }

Is returning false. I put a breakpoint and come to know that the navigational property is null that could be a reason for model not been valid. How can I surpass this. As you can see in code I have added a hidden field for binding navigational property

Why is this happening? Any help will be appreciated

As per the post ModelState.IsValid == false, why? provided by wiz kid

I come to know that tan exception is occuring there as

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)}

So I removed the line from view

@Html.HiddenFor(modelItem => modelItem[i].module)

This solved by problem

Community
  • 1
  • 1
Robert_Junior
  • 1,122
  • 1
  • 18
  • 40

0 Answers0