I have something like this:
public ActionResult Create(int clubid) { var club = db.Clubs.Single(c=>c.Id == clubid); ViewBag.Club = club; Competition comp = db.Competitions.Create(); return View(comp) }
and in my .cshtml:
@Model Models.Competition ... @Using(Html.BeginForm()) { ... <input type="submit" value="Save" /> }
This works fine with the following Post Action:
[HttpPost] public ActionResult Create(Competition comp) { if (ModelState.IsValid){...} return RedirectToAction(...); }
However, I want to pass an additional parameter from the @ViewBag.Club
object:
[HttpPoSt] public ActionResult Create(int clubid, Competition comp){...}
How do I code this in the BeginForm
?
Thanks for your help ! Multiple form tags should work fine in MVC unless they are nested.
It does hold two parameters, but those are for the controller name and the controller action.
The value will only be added as query string values if its FormMethod. Get (and you can remove the route values from the BeginForm() method). But what is the point of this - they are hidden inputs, not editable values.
BeginForm(HtmlHelper) Writes an opening <form> tag to the response. The form uses the POST method, and the request is processed by the action method for the view. BeginForm(HtmlHelper, String, String, Object, FormMethod, Object)
There are two options here.
Edit
@Html.Hidden("clubid", ViewBag.Club.id)
or
@using(Html.BeginForm("action", "controller", new { clubid = @Viewbag.Club.id }, FormMethod.Post, null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With