문제

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")%>
도움이 되었습니까?

해결책

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();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top