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

Categories

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

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

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...