In my ASP.NET MVC (5.2) project, I have a page called register.cshtml. It doesn't include any forms or anything, just plain divs.
Inside one of the divs, I'm rendering a partial:
#Html.Partial("~/Views/Users/_x.cshtml").
Inside _x.cshtml I have a form:
#using (Html.BeginForm("/users/x"))
{
...
}
When I go to my register page, I expect my form to be rendered as:
<form action="/users/x" method="post"> ... </form>
But instead, I'm getting this:
<form action="/users/register?Length=23" method="post" novalidate="novalidate"> ... </form>
What is length=23, why is there a novalidate attribute added, and why is it posting to an incorrect path?
Why is my form not rendering properly?
If your wanting to post to a method named x in usersController, then it needs to be
#using (Html.BeginForm("x", "users"))
{
....
}
Note that your currently using the overload that accepts object routeValues and because its a string, the method generated a route value for Length because that's the only property of string (the /users/register is because that the method that generated the main view)
From your code
Html.BeginForm("/users/x")
i understand that users your controller and x is a method. So you can do in this way-
#using (Html.BeginForm("x", "users", FormMethod.Post, new { id = "YourFormID"}))
{
}
#using (Html.BeginForm("action", "controller",new { QueryString = 1}, FormMethod.Post, null))
{
}
Note : its due to passing wrong parameter in beginform constructor .
and in ur VIEW
#Html.Partial("~/Views/Shared/_x.cshtml")
Related
I have a razor page that uses T4template. Here is my razor code:
#model ResearchViewModel
<form method="POST" action=">
...
#if (!Model.IsFinalized)
{
using (Html.BeginForm(MVC.Research.ActionNames.Reject, MVC.Research.Name, null, FormMethod.Post, new { #id = "RejectForm" }))
{
#Html.AntiForgeryToken()
#Html.HiddenFor(model => model.Id)
}
using (Html.BeginForm(MVC.Research.ActionNames.Accept, MVC.Research.Name, null, FormMethod.Post, new { #id = "AcceptForm" }))
{
#Html.AntiForgeryToken()
#Html.HiddenFor(model => model.Id)
}
}
...
</form>
The problem is that when the razor rendering this page, it cannot rendering first form !! I tried to change the sequence of these forms, and found that always the first form is not rendered. I Also tried to separate these forms using the partialview but the problem still exist. Does anyone knows that what's happening ?
You are trying to nest multiple forms and you can't do that. See this link for an explanation: Can you nest html forms?
You need to remove your starting HTML
<form method="POST" action=">
because you can't have other forms inside them. I would guess that closing tag of your first form created by razor Html helper is closing this tag, so you can see the other form created by second razor Html helper
I am inheriting a project which is somewhat built in Umbraco 6 and I am not familiar with Umbraco but learning thus far.
A partial view is using an existing template which effectively has this in its template:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = "MvcBanner.cshtml";
}
#section ContentPlaceHolderParent {
#Umbraco.RenderMacro("Breadcrumb")
#Umbraco.Field("pageName")
#Umbraco.Field("pageInstructions", insertBefore: "", insertAfter: "", convertLineBreaks: true)
#Html.Action(#Umbraco.Field("MVCActionName").ToString(), #Umbraco.Field("MVControllerName").ToString())
}
This template is being used by a page "UploadJobs.cshtml"
Now, on the UploadJobs.cshtml I have a few fields bound to a model and then a file upload:
#model Models.JobsModel
#using(Html.BeginUmbracoForm("UploadJobs", "Jobs"))
{
#Html.TextBoxFor(m => m.Name);
#Html.TextBoxFor(m => m.Files, new { type = "file", name = "Files" })
<input type="submit" value="Upload" id="cmdSubmitJobs" />
}
My action method looks like this:
[HttpPost]
[ValidateAntiForgeryToken]
public PartialViewResult UploadJobs(UploadJobs model)
{ ... }
When submitting, everything seems fine but when returning the model back to the view (i.e validation fails), it seems to break the page completely when rendering (i.e all styles and all formatting is gone) and any javascript//jquery functions I have returns errors when the document is being rendered
thoughts? I want to be able to obviously return the model if it is invalid
Ahmed,
your problem is probably because of you are redirecting to the partialview not the view itself. Double check your flow, the partial page is most likely called from another view (parent one). This parent view contains all required styling stuff within it.
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))
{
}
all, I could use your help again. I'm currently attempting to use an HTML helper to create a from that will pass my model into the Operations controller's method, TaskEdit, seen below:
[HttpPost]
public ActionResult TaskEdit(TaskViewModel viewModel, bool? embedded)
{
// code
}
In the view, I am using the following Razor code to attempt to produce the form:
#using (Html.BeginForm("TaskEdit", "Operations", new { embedded = true, viewModel = Model }, FormMethod.Post, new { #class = "form-horizontal" }))
{
// form code
}
This didn't actually give me my instance of the model - it only passed the class back as if it were a static class. So I tried the following instead:
#using (Html.BeginForm("TaskEdit", "Operations", new { embedded = true, id = Model.TaskId }, FormMethod.Post, new { #class = "form-horizontal" }))
{
// form code
}
And the following form was produced (which confused me):
<form action="/<sitename>/Operations/TaskEdit/0?embedded=True" class="form-horizontal" method="post"> <!-- Form code --> </form>
Not only was I assuming that the form action would be more along the lines of "/<sitename>/Operations/TaskEdit?id=0&embedded=True", but when I try to submit the form, I get a server error about "No parameterless constructor defined for this object." Help?
A transcription from the comments:
The reason the URL results in /TaskEdit/0 is because of the way your routing is setup. You'll notice the route also defines an id which causes it to format it different than expected.
The best solution here is to use a strongly typed view:
1) Put your model on top of the view (#model TaskViewModel)
2) Remove the model parameter from your form
3) Use the built-in extensions to create form fields (Html.EditorFor(x => x.SomeField)) or use the field names if you're doing it manually: <input type="text" name="SomeField />
4) The model parameter in your controller's actionresult will now contain the form data
I'm using ASP.NET MVC Preview 4 and would like to know how to use the routing engine for form submissions.
For example, I have a route like this:
routes.MapRoute(
"TestController-TestAction",
"TestController.mvc/TestAction/{paramName}",
new { controller = "TestController", action = "TestAction", id = "TestTopic" }
);
And a form declaration that looks like this:
<% using (Html.Form("TestController", "TestAction", FormMethod.Get))
{ %>
<input type="text" name="paramName" />
<input type="submit" />
<% } %>
which renders to:
<form method="get" action="/TestController.mvc/TestAction">
<input type="text" name="paramName" />
<input type="submit" />
</form>
The resulting URL of a form submission is:
localhost/TestController.mvc/TestAction?paramName=value
Is there any way to have this form submission route to the desired URL of:
localhost/TestController.mvc/TestAction/value
The only solutions I can think of are to create a separate action that just checks the request parameters, or to use Javascript.
Solution:
public ActionResult TestAction(string paramName)
{
if (!String.IsNullOrEmpty(Request["paramName"]))
{
return RedirectToAction("TestAction", new { paramName = Request["paramName"]});
}
/* ... */
}
In your route, get rid of the {paramName} part of the URL. It should be:
TestController.mvc/TestAction
As that is the URL you want the request to route to. Your form will then post to that URL.
Posted form values are mapped to parameters of an action method automatically, so don't worry about not having that data passed to your action method.
My understanding is that this is how HTML works. If you do a <form url="foo" method="get"> and post the form, then the form will post foo?
param1=value1&...¶mn=valuen
It has nothing to do with MVC.
Besides, what part of REST does that URL violate? It's not a pretty URL, but by strict definition of REST, it can be RESTful. REST doesn't specify that query parameters have to be in an URL segment. And in this case, those are query parameters.