Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

asp.net mvc - Pass multiple parameters in Html.BeginForm MVC

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There are two options here.

  1. a hidden field within the form, or
  2. Add it to the route values parameter in the begin form method.

Edit

@Html.Hidden("clubid", ViewBag.Club.id)

or

 @using(Html.BeginForm("action", "controller",
                       new { clubid = @Viewbag.Club.id }, FormMethod.Post, null)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...