1
votes

I want to pass user.Id to partial view to to populate "@Html.EditorFor(model => model.UserID, new { @Value = id })" in a Partial view. I am getting null in this Partial view Model. Code for parent view is

@model KhanewalNews.Models.user
    @{ 
        int id = Model.id;
    }
@Html.Partial("AssignRoles",null, new ViewDataDictionary { { "id", id } });

@{
    ViewBag.Title = "MemberDetails";
    Layout = "~/Views/Shared/_DashboardLayout.cshtml";
}
//parent view implementation here

And code for Partial view is

@model KhanewalNews.Models.Role
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
@{ 
    int id = (int)this.ViewData["id"];
}
    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <div class="editor-label">
                @Html.LabelFor(model => model.UserID)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.UserID, new { @Value = id })
                @Html.ValidationMessageFor(model => model.UserID)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.role1)
            </div>
            <div class="editor-field">
                @Html.DropDownListFor(m => m.role1, new SelectList(
                  new List<Object>{
                       new { value = "add" , text = "add"  },
                       new { value = "update" , text = "update" },
                       new { value = "delete" , text = "update"},
                        new { value = "super" , text = "super"}
                    },
                  "value",
                  "text"
           ))
                @Html.ValidationMessageFor(model => model.role1)
            </div>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>
    }
    <div>
        @Html.ActionLink("Back to List", "Index")
    </div>

this Throws the exception in a partial view that "System.NullReferenceException: 'Object reference not set to an instance of an object.'" Please guide me where i am wrong. Controller

public ActionResult AssignRoles()
        {
            return View();
        }
        [HttpPost]
public ActionResult AssignRoles(Role r)
        {
            //implementation here
            return View();
        }
    public ActionResult MemberDetails(int id)
    {
        var userdetail = new user();
        var user = db.users.Where(x => x.id == id).FirstOrDefault();
        userdetail.username = user.username;
        return View(userdetail);
    }
1
If you only pass the UserID to the partial, what about the other properties on in the partial page? Are they throwing the null reference exception? Try removing the other model properties in the partial page to identify what exactly is null. - L0uis
yes i did: @Html.Partial("AssignRoles",null, new ViewDataDictionary { { "id", id } }); but receive null. - Usman Kiani
Did you debug this yet? Are you sure the id is null or is it the model on your roles partial view? - Jerdine Sabio
@JerdineSabio thanks, while debugging i found that the id was not null. the problem was in my controller, i was sending incomplete user model. this code works fine now. - Usman Kiani

1 Answers

0
votes
public ActionResult MemberDetails(int id)
{
    var user = db.users.Where(x => x.id == id).FirstOrDefault();
    return View(user);
}