ASP.NET MVC partial view and form action name

0 votes
asked Apr 8, 2009 by dmitriy-shvadskiy

How do I create a partial view that has a form with assigned id? I got as far as:

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

Partial view is used for both Create and Edit so first parameter ?action? will be different. I can't figure out what value of ?action? supposed to be.


UPDATE:

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

 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" }))   

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

3 Answers

0 votes
answered Apr 8, 2009 by tvanfosson

Pass in the action to be performed via ViewData.

In your action that renders the view, create a ViewData item for the postback action. In your form reference this ViewData item to fill in 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.

Example 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";
     ...
}

Example using Model

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 (though I disagree), you could try some logic based off the ViewContext.RouteData

<%
    var action = "Create";
    if (this.ViewContext.RouteData.Values["action"] == "Edit")
    {
        action = "Update";
    }
    using (Html.BeginForm( action, "Candidate", ... 
    {
 %>
0 votes
answered Apr 8, 2009 by al-katawazi
<% html.RenderPartial("MyUserControl", Model.ID) %>
0 votes
answered Apr 21, 2010 by rodpl

Pass nulls as action and controller. Extension will use just current action and current controller

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

Action generated for form will be the same as 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

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

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

Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...