I have a edit View - Product/Edit/1
1 being the Id of the Product.How can I set the action of the edit post in the View to the POST edit action
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int Id, FormCollection collection)
The form tag is prepopulated as
but I want to set it to /Product/Edit/1
I am using this
<%using (Html.BeginForm()){ %>
but know its not right.Can someone help me how to set the form action using the htmlhelper class extension method to the Url in the browser
If you look at the intellisense for creating a Form with the HtmlHelper you will see there are parameters for specifying routeValues (of type object). Here you can specify the ID.
Your Edit View will be strongly typed with your Product object so you can specify Model.ID.
<% using (Html.BeginForm("Edit", "Product", new { Id = Model.ID } %>
...
Related
I have a view called "page.chtml" and I want to post from it to an action called "actionname"
[HttpPost]
public ActionResult actioname(...){...}
Is it possible?
Yes you can. In the action property of the form in page.cshtml simply specify actionname:
<form action="actioname">
you can use Html helper for creating a form with your desire submit action
#using (Html.BeginForm("actionName", "ControllerName", FormMethod.Post, new { id = "FormId", name = "FormName" }))
{
<div>//your page.cshtml inner html code goes here
}
If you want to post a form to a different action name, you can use the HTML helper for that. In Razor syntax it looks like this:
#using (Html.BeginForm("actioname"))
{
<!-- Form Fields -->
}
There are a few parameters you can use - the action is one, another is the controller in case you want to post to an ActionResult in a different controller than the one that handled the request for the page initially.
Yes You can use actioname in different controllers and post it:
in any view in any controller you can post:
#using (Html.BeginForm("actioname", "Controller", FormMethod.Post))
{
}
I have the following form
<form name="SearchForm" method="post" id="SearchForm" action="/Search/">
And the following button
<input type="button" onclick="javascript:document.SearchForm.submit();" class="btn-leftsearch">
On clicking this button, the form submits and calls this method
[HttpPost]
public ActionResult Index(string querystring)
{
return View();
}
Of course querystring is null. I want to pass querystring or preferably something else representing the fields in the form to the controller. I have tried playing with the action attribute in the form tag. I have tried to add the data to the onclick method in the button. Nothing is working. All I want to do is pass some data like this
Search?pri=all&amenity=pool etc
In the controller I would have something like
[HttpPost]
public ActionResult Index(string pri, List<string> amenities)
{
...
}
Can someone tell me how I can pass this data to the view?
I would like to suggest you that you can use the following code snip to resolve you problem.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(FormCollection collection)
{
string valueFromNameTextBox = collection["name"];
}
on the collection please put the name of the search text box. You wil get the actual entered value.
You can index into this collection with the names of all the inputs on the form.
I'm trying to add a webform that allows the user to add a database entry with a specific foreign key.
I'm creating the link like this
<%= Html.ActionLink("Edit", "EditSub", new { id = id }) %>
and the resulting URL is http://localhost:3015/TumourGroup/CreateSub/2 (where 2 is the id I passed to the actionlink earlier). The question is, how do I retrieve the value of id from the URL? I'm using it to grab the "main" table so that I can create a new table entry that has a foreign key pointing to the "main" table.
Assuming that TumourGroup is the name of a controller, and you have a route that looks something like this:
routes.MapRoute(
"Default",
"{controller}, {action}, {id}",
new { controller="Home", action="Index", id="" }
)
Then in your TumourGroup controller, you just need a controller method that looks like this:
public ActionResult CreateSub (int id)
{
// blah
}
The parameter id will contain your id from the Url.
EDIT: To include the id when you are submitting a form:
public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)
{
// blah
}
Add the id as a property to your TumorGroupSubcategory class.
In the form view you are submitting, include a hidden field that is named the same as the id in your TumorGroupSubcategory class, and populate it with your id field.
When your user submits the form, the Model Binder will pick up the field, and put it into the id property of tumourSubgroupToCreate automatically.
Have your controller function CreateSub take in int id
public ActionResult CreateSub (int id)
If you want to go to the form with this id, then post with a different set of data, you'd need two functions, differentiated by
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult CreateSub (int id)
The get is for navigating to your entry form, the post is called when the form posts.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult CreateSub (TumourGroupSubcategory tumourSubgroupToCreate)
BELOW IS RESPONDING TO CLARIFICATION IN COMMENTS:
Well, if you forego the strongly typed view, you can just do
ViewData["id"] = id;
ViewData["subGroupToCreate"] = ...
Alternatively you can do it in the second form on the client side with Javascript or Jquery
If you look at the NerdDinner example of creating and editing dinners then you see they use a partial (ViewUserControl or ASCX) DinnerForm to put the functionality of creating and editing dinners into one file because it is essential the same and they use it using RenderPartial("DinnerForm").
This approach seems fine for me but I've run into a problem where you have to add additonal route values or html properties to the Form tag.
This picks up the current action and controller automatically:
<% using (Html.BeginForm()) { %>
However, if I use another BeginForm() overload which allows to pass in enctype or any other attribute I have to do it like this:
<% using ("Create", "Section", new { modal = true }, FormMethod.Post, new { enctype = "multipart/form-data" }))
and as you can see we lose the ability to automatically detect in which View we are calling RenderPartial("OurCreateEditFormPartial"). We can't have hardcoded values in there because in Edit View this postback will fail or won't postback to the right controller action.
What should I do in this case?
What about adding the following HTML helpers:
public static MvcHtmlString CurrentAction(this HtmlHelper htmlHelper)
{
return htmlHelper.ViewContext.RouteData.Values["action"];
}
public static MvcHtmlString CurrentController(this HtmlHelper htmlHelper)
{
return htmlHelper.ViewContext.RouteData.Values["controller"];
}
Then you could go something like this:
<% using (Html.CurrentAction, Html.CurrentController, new { modal = true }, FormMethod.Post, new { enctype = "multipart/form-data" }))
Its not 100% perfect but you could also add an additional HTML helper which would streamline it a little more:
public static MvcForm BeginForm(this HtmlHelper htmlHelper, object routeValues, FormMethod method, object htmlAttributes)
{
return htmlHelper.BeginForm(Html.CurrentAction, Html.CurrentController, routeValues, method, htmlAttributes);
}
Let me know if this helps.
Cheers
I'm answering in an old thread because it came up number two on a Google search whilst I was looking for the same thing :) Found on this SO post:
Html.BeginForm and adding properties
With MVC3 (not sure about MVC2) you can pass null in for the action and controller and get the default routes that BeginForm() would use.
#Html.BeginForm(null, null, FormMethod.Post, new { enctype="multipart/form-data"})
Cheers!
I have a dropdownlist that I populate with some stuff:
In my controller
ViewData["SourceModelList"] = new SelectList(_modelService.GetAllModels(), "Id", "Description");
in my view
<% using (Html.BeginForm("Compare", "Home")) { %>
<p>
<%=Html.DropDownList("SourceModelList")%>
</p>
<p>
<input type="submit" value="Compare" />
</p>
<% } %>
And this renders lovely. Now when I post back to my 'compare' action, how do I find out which item was selected in the drop down?
The name "SourceModelList" should correspond with the name of a field in your ViewModel, so that the binder has something to bind the value of the dropdown to.
Alternatively, you can pluck the value out of the FormCollection object, if your view is not strongly-typed.
The NerdDinner Tutorial goes into this process in greater detail:
NerdDinner Step 5: Create, Update, Delete Form Scenarios
http://nerddinnerbook.s3.amazonaws.com/Part5.htm
You can use any of the regular methods for getting items from a form in ASP.NET MVC: FormCollection, Request object, binding to a specific model or having an action which takes a string SourceModelList parameter.
You can do:
int value = Convert.ToInt32(Request.Form["SourceModelList"]);
Or by ModelBinders just making sure that your model have a property
public int SourceModelList {get; set;}
And the ModelBinder will get it for you.
Or, but less likely:
public ActionResult Name(FormCollection f, int SourceModelList)