MVC HTML.Beginform Different Url Schema

ASP.NET MVC - Post form to html bookmark?

Try like this:

<form action="<%: Url.Action("actionName", "controllerName") %>#errors" method="post">

<%:Html.HiddenFor(model => model.SourceUrl)%>
<%:Html.HiddenFor(model => model.EulaId)%>

<a name="accept"></a>
<div style="text-align:center;">
<%:Html.CheckBoxFor(model => model.Accepted)%>
<%:Html.LabelFor(model => model.Accepted)%>
</div>
<input type="submit" value="Submit">

</form>

You could also write a custom HTML helper that will do the job:

public static class FormExtensions
{
public static MvcForm MyBeginForm(this HtmlHelper htmlHelper, string actionName, string controllerName, string fragment)
{
var formAction = UrlHelper.GenerateUrl(null, actionName, controllerName, htmlHelper.ViewContext.HttpContext.Request.Url.Scheme, null, fragment, null, htmlHelper.RouteCollection, htmlHelper.ViewContext.RequestContext, true);
var builder = new TagBuilder("form");
builder.MergeAttribute("action", formAction);
builder.MergeAttribute("method", "post", true);
htmlHelper.ViewContext.Writer.Write(builder.ToString(TagRenderMode.StartTag));
return new MvcForm(htmlHelper.ViewContext);
}
}

And then:

<% using (Html.MyBeginForm("index", "home", "errors")) { %>
...
<% }

Ajax.BeginForm absolute url MVC CORS

Is there any way to get the Ajax.BeginForm to output a absolute url
rather than just a relative one?

Yes, but you need to specify the Url in the AjaxOptions and not use the overload which takes action and controller names:

@using (Ajax.BeginForm(new AjaxOptions { Url = Url.Action("Foo", "Bar", null, "http") }))
{
...
}

Url.Action has an overload where you could specify the protocol to be used which generates absolute urls. And if you wanted to use the same protocol as the one used to request the current action instead of hardcoding it you could use the following:

@using (Ajax.BeginForm(new AjaxOptions { Url = Url.Action("Foo", "Bar", null, Request.Url.Scheme) }))
{
...
}

How to generate a URL for ASP.NET MVC action, including hostname and port?

Sample Image

Url.Action("Action", "Controller", null, Request.Url.Scheme);

How to include the following in the Form Action attribute?

1. Protocol(http:// or https://)
2. HostName

3. QueryString

4. Port

@{
var actionURL = Url.Action("Action", "Controller",
FormMethod.Post, Request.Url.Scheme)
+ Request.Url.PathAndQuery;
}
@using (Html.BeginForm("Action", "Controller", FormMethod.Post,
new { @action = actionURL }))
{
}

Passing params with @Html.Beginform

I am assuming from your code that this is an Asp.Net MVC project, using C# as the language.

There are a number of issues. The first is that the equipmentID variable is not being passed to the view. There are a number of ways to do this. The simplest will be to do it in your controller using the ViewData dictionary, as follows:

public ActionResult CreateReservation(int equipmentid)
{
// TODO: Query the piece of equipment being reserved.

ViewData["equipmentID"] = equipmentid;

return View();
}

Once this is done, you could modify your view code to use the value as follows:

@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { equipmentID = ViewData["equipmentID"] }))
{
<button class="btn btn-blue">Click</button>
}

This is fine for one or two values, but when the view gets more complex, you rather want to create a ViewModel class, and pass that class to your view.

Secondly - your use of the @ in front of equipmentID inside your BeginForm call indicates that you probably need to study up a bit on Razor syntax. A good place to start is Phil Haack's Razor Syntax Quick Reference. Razor syntax is what tells the parser which parts of your view are static HTML that it should output as is, and which parts are C# code that it should execute before sending to the browser. If you are using Visual Studio as your IDE, it is pretty good at highlighting which parts of your code are being interpreted as C# code, and which parts as ordinary HTML.

In my screenshot below you can see that VS2012 highlights C# code with a light grey background colour. The @ symbol is the start of C# code - so you don't need to use it again unless you break out of the C# block and go back to HTML.

Example of how Razor C# code is highlighted in Visual Studio 2012

And thirdly, you probably wouldn't want to pass the equipment ID as an HTML attribute inside your form tag. You should rather create a hidden form field inside your form body and set the name to equipmentID, and the value to the variable's value. The way you are currently using it will add an HTML attribute to the form tag, and your generated code will look something like this:

<form action="/ReservationList/CreateReservation" method="get" equipmentID="1">

And this extra attribute will not be retrievable in your code. Rather do something like this in your view:

@using (Html.BeginForm("CreateReservation", "ReservationList", FormMethod.Get, new { name = "myFormName", id = "myFormID" }))
{
@Html.Hidden("equipmentID", ViewData["equipmentID"])
<button class="btn btn-blue">Click</button>
}

In ASP.NET MVC Should A Form Post To Itself Or Another Action?

For a RESTful controller:

// return an HTML form for editing a specific entity
public ActionResult Edit(int id) { }

// find and update a specific entity
[HttpPut]
public ActionResult Update(EditModel userView) { }

And in the View:

<% using (Html.BeginForm<HomeController>(c => c.Update(null))) {%>
<%: Html.HttpMethodOverride(HttpVerbs.Put) %>
<%: Html.EditorForModel() %>
<input type="submit" value="Save" />
<% } %>


Related Topics



Leave a reply



Submit