ASP.NET MVC partial view and form action name

How to create a partial view having a form with an assigned identifier? I got to:

using (Html.BeginForm(?action?,"Candidate",FormMethod.Post,new {id="blah"})) 

A partial view is used for both Create and Edit, so the first parameter is ?action? will be different. I can not understand what is the meaning of ?action? should be.


UPDATE:

I think I was not clear enough with the question. What I ended up with is splitting Request.RawUrl to get the name of the controller and the name of the action:

  string[] actionUrlParts = ViewContext.HttpContext.Request.RawUrl.Split('/'); using (Html.BeginForm(actionUrlParts.Length >= 2? actionUrlParts[2] : "", actionUrlParts.Length >= 1 ? actionUrlParts[1] : "", FormMethod.Post, new { id = "blah" })) 

The view is ugly, but it works. Is there a better way to get the name of an action inside a partial view?

+4
source share
3 answers

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", ... { %> 
+7
source

Pass null values ​​as an action and controller. The extension will only use the current action and the current controller.

 using (Html.BeginForm(null, null, FormMethod.Post, new { id="Model" })) 

The action created for the form will be the same as the parent view of this partial view.

It generates

 <form action="/Orders/Edit/1" id="Model" method="post"> 

for URL http: // localhost: 1214 / Orders / Edit / 1

... and this one

 <form action="/Orders/Create" id="Model" method="post"> 

for URL http: // localhost: 1214 / Orders / Create

+1
source
 <% html.RenderPartial("MyUserControl", Model.ID) %> 
0
source

All Articles