Question

I'm using Html.BeginForm and trying to pass the supplied value of the textBox "archName" to the post, How can I do that? I mean what should I add instead of "someString"?

<% using (Html.BeginForm("addArchive", "Explorer", new { name = "someString" }, FormMethod.Post)) { %> 
    <%=  Html.TextBox("archName")%>
Was it helpful?

Solution

The name that you are referring to is the name attribute of the form HTML element, not posted values. On you controller you can access a few ways.

With no parameter in controller method:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive()
{
    string archName = HttpContext.Reqest.Form["archName"]
    return View();
}

With the FormCollection as parameter in controller method:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive(FormCollection form)
{
    string archName = form["archName"];
    return View();
}

With some model binding:

//POCO
class Archive
{
    public string archName { get; set; }
}

//View
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Namespace.Archive>" %>    
<%= Html.TextBoxFor(m => m.archName) %>

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult addArchive(Archive arch)
{
    string archName = arch.archName ;
    return View();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top