简体   繁体   中英

How to pass the ids in model class in view to the controller along with some form data?

查看页面是这样的 Here am pressing the pay button i have to pass ids and the form data in my view am displaying some data using for each loop,and when i click a button i have to pass the number of ids which returns in model class and some form data(which is outside the model class) to the controller,i have tried my maximum,But i failed,please help me to make this?

view.cshtml

@foreach (var item in Model) {
    <input type="hidden" value="@item.vid" name="abc" />//i need these ids in my controller
    <tr>

        <td>

        <td> @Html.DisplayFor(modelItem => item.vid)</td>
       .........
         ...........//some more displaying
}
@Html.BeginForm("makepayment","home",FormMethod.Post)
{
<h3>Card Holder Name</h3>
        <input type="text" name="cname" />

       <h3>Account Name</h3>
  <input type="text" name="number" />
       <h3>CVC</h3>
     <input type="text" name="securitycode" />

       <h3>Expiery Date</h3>
     <input type="text" name="expdate" />



           <input type="submit" id="sub" name="sub" value="pay" />                    
  }

controller

public ActionResult makepayment(FormCollection values,????)
         {
......
.......//codes
}

i know i may have to use array or something,but i dont know how,somebody please help

Now, following semantics, you should create a separate viewmodel class that can hold such information, but that is not always the case. So here is my understanding. You have a model class which you are what your form consists of, but you also want to send the ids that are just related to something else. In you post action, you get an instance of your model class with model binding.

//Please ignore the [] chars
public ActionResult makepayment([Name of you model class] modelInstance)
{
    //modelInstance will contain the fields of the model.
    //The remaining field, you can access using.
    //Request.Form["{name of the input field}"]
    var id = Request.Form["abc"]
}

Hope this helps you out. Otherwise, hit me with up and we can easily go through it in more detail.

P.S :- I am not a sir.

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