J Henry
J Henry

Reputation: 23

I keep getting a null entry when passing data from view to view Asp.net mvc?

I'm trying to pass data between views and I keep getting a null value when I try to pass it through.

All I want is for the actionlink from "contractordetails" view to pass its contractorid on the page to the next view "contractorrequest" so it can be displayed.

The UserId and User Name is working I just keep getting an error as shown below indicating its a null entry.

enter image description here

Where am I going wrong?

My code:

      @Html.ActionLink(
                       linkText: "Contractor Area",
                       actionName: "ContractorRequest",
                       controllerName: "ConDashboard",
                       routeValues: new {area = "Contractor",id = Model.ContractorId},
                                                     htmlAttributes: null )

Controller

    [HttpGet]
    public ActionResult ContractorRequest(ContractorVM model, int contractorid)
    {

        int id = model.ContractorId;

        var addrequest = new AddCalendarRequestVM();
        addrequest.User = new UserVM();

        string username = User.Identity.Name;



        using (Db db = new Db())
        {

            UserDTO dto = db.Users.FirstOrDefault(x => x.Username == username);

            addrequest.User = new UserVM(dto);
        }





        return View("ContractorRequest", addrequest);
    }

View

   @using (Html.BeginForm("ContractorRequest", "ConDashboard", FormMethod.Post, new { enctype = "multipart/form-data" }))
                        {
                            @Html.AntiForgeryToken()

                        <form class="form-horizontal">



                            <hr />
                            @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                            <div class="form-group">
                                @Html.LabelFor(model => model.User.UserId, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model.User.UserId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                                    @Html.ValidationMessageFor(model => model.User.UserId, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.User.Name, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model.User.Name, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                                    @Html.ValidationMessageFor(model => model.User.Name, "", new { @class = "text-danger" })
                                </div>
                            </div>

                            <div class="form-group">
                                @Html.LabelFor(model => model.Contractor.ContractorId, htmlAttributes: new { @class = "control-label col-md-2" })
                                <div class="col-md-10">
                                    @Html.EditorFor(model => model.Contractor.ContractorId, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                                    @Html.ValidationMessageFor(model => model.Contractor.ContractorId, "", new { @class = "text-danger" })
                                </div>
                            </div>

Upvotes: 0

Views: 63

Answers (1)

Hooman Bahreini
Hooman Bahreini

Reputation: 15559

What you are trying to do does not sound right... you want to send a get request to your controller action, but the action is expecting a model to be passed in... normally when you want to pass a model to action method, you use a form and post content of the form to the action method.

So if you want to send your model to the action method, then use a form and post method (not get).


If all you want to pass to action method is an id, then remove ContractorVM model from the action method... the error that you are getting is because ContractorVM has non-nullable id and you are not passing any id for it.

So this link:

@Html.ActionLink(
    linkText: "Contractor Area",
    actionName: "ContractorRequest",
    controllerName: "ConDashboard",
    routeValues: new {area = "Contractor", id = Model.ContractorId},
    htmlAttributes: null)

should hit the action:

[HttpGet]
public ActionResult ContractorRequest(int id) // <-- make sure parameter name matches what you are passing in
{
    // more code
    // ...
}

Upvotes: 3

Related Questions