如何在动作方法ASP.NET mvc中访问我的formcollection?

时间:2011-08-09 11:15:50

标签: asp.net-mvc

我在动作方法中访问了表单集合,但是如何获取它的值。我试过这样的

string value = collection[1];

但我没有得到这个值。如何在操作方法中访问该值。

7 个答案:

答案 0 :(得分:28)

如果你有:

<input type="text" name="inputName" />

您可以使用以下元素的属性名称:

[HttpPost]
public ActionResult yourAction(FormCollection collection)
{
     string value = Convert.ToString(collection["inputName"]);
     ...
     return View();
}    

答案 1 :(得分:2)

我认为如果你能够支持强类型视图模型,你应该尝试清除formcollection对象。这里有几个例子,我联系了我搜索的第一个:

passing FormCollection to controller via JQuery Post method and getting data back...

然而,如果你渴望将自己束缚在结:),那么这里是一个迭代一个formcollection的例子:

http://stack247.wordpress.com/2011/03/20/iterate-through-system-web-mvc-formcollection/

答案 2 :(得分:0)

像(未经过测试的代码) -

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddNewLink(FormCollection collection)
{
    string url = collection[1].ToString();
}

答案 3 :(得分:0)

创建一个这样的视图

<form action="/myController/myAction" method="post">
User Name <input type="text" name="userName" /> <br>
Country <input type="text" name="country" /><br>
<input type="submit" value="submit" />
</form>

创建如下所示的行动

public ActionResult myAction(string userName, string country){
      //do some thing with userName
      //asp.net mvc3 has automatically bind that for you
}

注意:以上编写的代码不是推荐的做事方式,仅用于演示。

答案 4 :(得分:0)

请尝试这个例子,希望它会有所帮助......

 public class UserName
    {        
       public string FName { get; set; }
       public string LName{ get; set; }               
    }


        [HttpGet]
        public ActionResult FormCollectionEg()
        {
            return View();
        }

        [HttpPost]
        public ActionResult FormCollectionEg(FormCollection data)
        {
            UserName UserObj = new UserName();
            UserObj.FName = data["fname_name"];
            UserObj.LName = data["lname_name"];
            return  RedirectToAction("DisplayFormCollectionData", UserObj);           
        }


        public ActionResult DisplayFormCollectionData(UserName reg)
        {
             return View(reg);
        }

创建两个视图 - DisplayFormCollectionData FormCollectionEg

<强> DisplayFormCollectionData

@model YourProjectNamespace.Models.UserName

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>DisplayFormCollectionData</title>
</head>
<body>
    <div>
        <h4>User Deatails</h4>
        <hr />
        <dl class="dl-horizontal">
            <dt>
                @*@Html.DisplayNameFor(model => model.FName)*@
                First Name....
            </dt>

            <dd>
                @Html.DisplayFor(model => model.FName)
            </dd>

            <dt>
                @*@Html.DisplayNameFor(model => model.LName)*@
                Last Name...
            </dt>

            <dd>

                @Html.DisplayFor(model => model.LName)
            </dd>

        </dl>
    </div>
    <p>
        @*@Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) |
        @Html.ActionLink("Back to List", "Index")*@
    </p>
</body>
</html>

<强> FormCollectionEg -

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>FormCollectionEg</title>
</head>
<body>
    @using (Html.BeginForm("FormCollectionEg", "Home"))
    {
        <table>
            <tr>
                <td>Enter First Name</td>
                <td><input type="text" id="fname_id" name="fname_name" /></td>
            </tr>
            <tr>
                <td>Enter Last Name</td>
                <td><input type="text" id="lname_id" name="lname_name" /></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" /></td>
            </tr>
        </table>



    }

</body>
</html>

答案 5 :(得分:0)

我首选的选项是使用UpdateModel

此MVC方法不会手动映射字段,而是自动绑定请求中可用数据的属性,就像您将严格类型作为参数传递给操作一样。

[HttpPost]
public ActionResult FormCollectionEg()
{
    var model = new Username();

    UpdateModel<Username>(model);

    return View(model);
}

上面的代码将包含来自QueryString的数据以及可能不合适的表单数据。如果你在动作中使用了一个参数,你可以使用[FromBody]来限制它,但是使用UpdateModel你仍然可以通过传入FormCollection作为值提供者来实现同样的目的。

[HttpPost]
public ActionResult FormCollectionEg(FormCollection collection)
{
    var model = new Username();

    UpdateModel<Username>(model, collection);

    return View(model);
}

答案 6 :(得分:0)

查看您的HTML文件(或使用Inspect Element)以查看输入字段中的name属性。 例如,如果您有

<input type = "text" name = "YourName"/>

在控制器中,

[HttpPost]
public ActionResult ActionName(FormCollection fc)     /*Using FormCollection*/
        {
                string variable_name = fc["YourName"];
                return View();
        }

FormCollection是在控制器中检索视图数据的一种方法。根据输入值的类型,可以在Action方法中将其非字符串值解析为字符串。