简体   繁体   中英

How to pass model and another parameter to a view

I may have done this already... and I might just be missing something simple, but essentially I just want to be able to do this...

public ActionResult ContinueProcess(Model m, int id){


}

I just want to pass the model and another parameter at the same time. Any thoughts? Thanks!

Either with a ViewModel

public class MyViewModel {
    public Model MyModel {get;set;}
    public int ParameterId {get;set;}
}

used as Model of your View,

or with a ViewBag

ViewBag.ParameterId= id;

I would certainly create a ViewModel to handle this

public class ContinueProcessViewModel
{
   public int ID { set;get;}
   public Model ContinueProcess { set;get;}
}

Return this ViewModel in your get action method'

public ActionResult ContinueProcess(int id)
{
  ContinueProcessViewModel objVM=new ContinueProcessViewModel();
  objVm.ID=id;
  return View(objVM);
}

And in your View, Have a HTMLelement(textbox/hidden) to hold the value of this

@Html.HiddenFor(x=>x.ID)

Now you should be able to acceess this in your httppost action method

[HttpPost]
public ActionResult ConitnueProcess(ContineProcessViewModel objVM)
{
  //Check objVm.ID here
}

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