简体   繁体   中英

Model not being passed to rendered view

I am having an issue getting data in my model on my MakePayment.cshmtl view.

The AccountScreen.cshtml is calling the MakePayment.cshtml view:

@model SuburbanCustPortal.SuburbanService.CustomerData      

@{
    ViewBag.Title = "Account Screen";
 }

<h2>AccountScreen</h2>

<div class="leftdiv">
  <fieldset>
    <legend>customer info</legend>
    @Html.Partial("CustomerInfoPartialView", Model)
  </fieldset>

  <fieldset>
    <legend>delivery address</legend>
    @Html.Partial("DeliveryAddressPartialView", Model)
  </fieldset>

  <fieldset>
    <legend>delivery info</legend>
    @Html.Partial("DeliveryInfoPartialView", Model)
  </fieldset>
</div>

<div class="rightdiv">
  <fieldset> 
    <legend>balance</legend>
    <div>
      @Html.Partial("BalancePartialView", Model)
    </div>    
  </fieldset>

  <fieldset> 
    <legend>payment</legend>
    <div>
      @Html.Partial("MakePayment", Model)
    </div>    
  </fieldset>

  <fieldset> 
    <legend>billing info</legend>
    <div>
      @Html.Partial("BillingInfoPartialView", Model)
    </div>    
  </fieldset>
</div>

My MakePayment.cshtml view:

@model SuburbanCustPortal.SuburbanService.CustomerData

  @using (Html.BeginForm("MakePayment2", "Customer", FormMethod.Post))
  {
    <div style="text-align:center;">
      <input class="makePaymentInput" type="submit" value="Make a Payment" />
    </div>       
  }

My CustomerController:

public ActionResult AccountScreen(LogOnModel model)
{
  return ShowCustomer(model.AccountNumber);
}

public ActionResult MakePayment(CustomerData model)
{
  return View("MakePayment", model);
}

[HttpPost]
public ActionResult MakePayment2(CustomerData model)
{
  //CustomerData model = new CustomerData();
  var newmodel = new PaymentModel.SendToGateway();
  newmodel.AccountBalance = model.TotalBalance;
  newmodel.Amount = model.TotalBalance;

  return RedirectToAction("PrePayment", "Payment", newmodel);
}

The public ActionResult MakePayment(CustomerData model) is never being reached.

My problem: The [HttpPost] public ActionResult MakePayment2(CustomerData model) is being reached but the model has nulls in it.

I know the data initial model from the AccountScreen is being populated since the other views that are being rendered is showing data.

Anyone see what I am doing wrong?

The problem is there's nothing inside your form except a submit button. You need to make sure input fields are there (either text boxes, select lists, or hidden fields), as those are what post data back to the controller.

You could try using EditorForModel inside your partial view:

@using (Html.BeginForm("MakePayment2", "Customer", FormMethod.Post))
{
    @Html.EditorForModel()
    <div style="text-align:center;">
      <input class="makePaymentInput" type="submit" value="Make a Payment" />
    </div>       
}

Edit based on comments

Razor doesn't include an Html.HiddenForModel() method, for whatever reason. Possible workarounds:

Related quesion here:

The problem is, you are creating a form containing nothing else than a submit button.

When you submit it, it posts nothing back to the server, thus your function receives an empty model.

@using (Html.BeginForm("MakePayment2", "Customer", FormMethod.Post))
  {
    <div style="text-align:center;">
      <input class="makePaymentInput" type="submit" value="Make a Payment" />
    </div>       
  }

This translates as :

<form method="POST" action="{url}">
    <div style="text-align:center;">
      <input class="makePaymentInput" type="submit" value="Make a Payment" />
    </div>
</form>

More details :

Since in the logic you then redirect to a new page to collect payment information, you don't want to give the user the opportunity to mess with your model, thus you should query your customer data from your Context instead of trusting what is submitted in the POST.

Thus all you really need to add if this :

@using (Html.BeginForm("MakePayment2", "Customer", FormMethod.Post))
{
   @Html.HiddenFor(model => model.{ID Field})
   <div style="text-align:center;">
     <input class="makePaymentInput" type="submit" value="Make a Payment" />
   </div>       
}

This way, you will be able to get your model back in the server side code.

Basically, your form submits nothing as there are no input fields inside the form scope. Try to wrap all your html in AccountScreen.cshtml within @using (Html.BeginForm( statement (and throw it out from MakePayment.cshtml ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM