ASP.NET MVC usercontrol create and edit mode setting - asp.net-mvc

I'm using an ASP.NET MVC usercontrol to represent a form for both a "create" mode and an "edit" mode.
The form involves a file upload so looks something like this:
<% using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
How can I make it post to the Create action in create mode and Edit action in edit mode?
I'm rendering it in the View like this:
<% Html.RenderPartial("NewsForm"); %>

Simple. Don't specify the action name in your call to Html.BeginForm. By default the same action name is used for POST as for GET (and similarly for controller). So if you just do:
<% using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
...then you get what you want.
A general rule of thumb for ASP.NET MVC is, "If your code is not doing what you want, try removing some of it."

The best way is to take all the fields and put them into a partial view and have the main view (Create/Edit) do the different begin forms.

Related

Html.BeginForm with overload causes form action to be '/cgi-bin?action=Index&controller=Home' and so the HomeController is bypassed

Because I wanted to assign an id to a form element my code was #using (Html.BeginForm(null, null, FormMethod.Post, new { id = "indexform"}))
This resulted in markup <form action="/cgi-bin?action=Index&controller=Home" id="indexform" method="post" style="position: relative;">
My HomeController ActionResult Index was completely bypassed.
Notice the '/cgi-bin'. This might be the problem and I think the culprit may be parameter 'FormMethod.Post' but I could not enter null there.
(BTW I worked around the id requirement by using jQuery var form = $("#btnShowProperty").closest("form");)
I did quite a bit of Googling on this with no luck.
The problem is you're passing null for the action and controller in the first two arguments. Instead, pass the name of your action and controller and it will work.
Example, if your action is "Index" and your controller is "Home", then use:
#using (Html.BeginForm("Index", "Home", FormMethod.Post, new { id = "indexform"})) {
...
}
The problem is mixing web pages and mvc especially using MapPageRoute AND MapRoute in Route.config. See full answer #Url.Action(“Action”, “Controller”) returns “/cgi-bin?action=Action&controller=Controller”

Posting a form from a partial view

I have a modal which I will be using extensively on many different pages and hence I thought putting it in a partial view was appropriate. Within the modal is a form but I'm just getting a 'can't find action on controller error' whenever I try submit the form.
#using (Html.BeginForm("AddPlayerSignup", "SignupSheet", new {id = Model.Id, slug = Model.Slug}))
{
#Html.DropDownList("Member", Model.SignupSheet.GroupMembers, new {label = "Members"})
#Html.Hidden("SignupSheetId", Model.SignupSheet.Id)
#Html.Hidden("ModalId", Model.ModalId)
#Html.SubmitButton("Add", true, new {#class = "btn btn-primary"})
}
The weird thing is it's not even saying it can't find addplayersignup on signupsheetcontroller, it's saying it cant find list on ladder which is the source of the partial view.
Is posting forms from a partial view not supported or something?
It looks like you forgot to add a form method,...
#using (Html.BeginForm("AddPlayerSignup", "SignupSheet",FormMethod.Post ,new {id = Model.Id, slug = Model.Slug}))
That should do the trick

How to check enctype at the MVC Controller?

Is it possible to check at the controller action to see if the data are posted from a enctype="multipart/form-data form? How?
Update: I got a form with file upload.
#using (Html.BeginForm("Create", "Report", FormMethod.Post, new {#class = "form-horizontal", enctype = "multipart/form-data"}))
{
....
}
On the controller, to accept the POST.
[HttpPost]
public ActionResult Create(InputModel model){
....
}
Is there anyway we can tell if the data posted to this action is from a multipart enctype form or a regular form?
Say, there is a regular form on another page, which has no file upload. It wants to post to the same action.
Not that good idea, but you can try it.
Add a hidden property at the View like
#Html.HiddenFor(m => m.HiddenPropertyName)
then check the value at controller
if(model.HiddenPropertyName == "someValue")
// upload file or something
else
// do your work

Httppost different named view and action method

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

How can I name a form with Html.BeginForm()?

How do I give a name to a form in ASP.NET MVC using Html.BeginForm()? I want only the name, not the action or controller name because I want to post it through Javascript. I think it should be something like Html.BeginForm(id = "frm").
I tried the following:
Html.BeginForm(null,null,new{id="frm",name="frm})
Html.BeginForm(new{#id="frm",#name="frm})
But the above code produces output like this:
<form action="/Main/Index/Id?name=Id" method="post">
Html.BeginForm(null, null, FormMethod.Get, new { name = "frm", id = "frm" })
You'll need to catch the form submit with your JavaScript
Using MVC2, this is what worked for me:
<% using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new {#id = "myForm", #name = "myForm"})) {%>
#HTML.BeginForm("Target-ViewName where you want to post the page","Controller Name",new {#name="Name of the Form", id="ID of the Form"})
{ //form here
}
Taken from this answer: How to pass in ID with Html.BeginForm()?
Can you not just do:
Html.BeginForm(new {#id="Id", #name="Id"});
It can pay to search SO for answers as I've found many things I want to ask have already been encountered.

Resources