Pass the action to be performed through ViewData.
In your action that displays the view, create a ViewData element for the postback action. In your form, specify this ViewData element to populate the action parameter. Alternatively, you can create a view-only model that includes the action and the actual model as properties and reference it from there.
An example of using ViewData:
using (Html.BeginForm( (string)ViewData["PostBackAction"], "Candidate", ...
Rendering Actions:
public ActionResult Create() { ViewData["PostBackAction"] = "New"; ... } public ActionResult Edit( int id ) { ViewData["PostBackAction'] = "Update"; ... }
Model Example
public class UpdateModel { public string Action {get; set;} public Candidate CandidateModel { get; set; } } using (Html.BeginForm( Model.Action, "Candidate", ...
Rendering Actions:
public ActionResult Create() { var model = new UpdateModel { Action = "New" }; ... return View(model); } public ActionResult Edit( int id ) { var model = new UpdateModel { Action = "Update" }; model.CandidateModel = ...find corresponding model from id... return View(model); }
EDIT . Based on your comment, if you feel that this should be done in the view (although I disagree), you can try some kind of logic based on ViewContext.RouteData p>
<% var action = "Create"; if (this.ViewContext.RouteData.Values["action"] == "Edit") { action = "Update"; } using (Html.BeginForm( action, "Candidate", ... { %>
source share