Guerrilla
Guerrilla

Reputation: 14846

Model not getting passed to controller

I have this index view:

@model LeadManager.Models.ProspectingApprovalViewModel

@{
    ViewBag.Title = "Index";
}

<h2>Approve Or Reject</h2>
<div class="form-horizontal">
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="form-group">
        <div>
            <input type="submit" name="ApprovalAction" value="Approve" class="btn btn-default" />
            <input type="submit" name="ApprovalAction" value="Reject" class="btn btn-default" />
        </div>
    </div>
}
</div>

//model details are below form

I am trying to read the model inside controller like so:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(ProspectingApprovalViewModel prospectingApprovalViewModel, string ApprovalAction)
{
    //access model
}

I can read ApprovalAction but the prospectingApprovalViewModel is null on the postback.

Why is the model not being attached?

Upvotes: 0

Views: 56

Answers (1)

Behzad Ahmadkhanloo
Behzad Ahmadkhanloo

Reputation: 31

  1. Your helper must be inside the form.
  2. When you use two class in parameter in name of html helper must be name="className.propetyName".
  3. When you want to post two classes you must use ViewModel.

Upvotes: 1

Related Questions