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

时间:2021-05-13 03:32:47

I have form collection accessed in action method ,but how to get the value of it .I tried like this

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

string value = collection[1];

but I am not getting the value .How can I access the value in action method.

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

6 个解决方案

#1


24  

If you have:

如果你有:

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

You could use the attribute name of the element like below:

您可以使用元素的属性名称,如下所示:

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

#2


3  

I think you should attempt to stear clear of the formcollection object if you are able to, in favour of a strongly typed viewmodel. there are a few examples here on SO and i've linked the 1st one that I searched for:

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

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

通过JQuery Post方法将FormCollection传递给控制器​​并获取数据...

however, if you're keen to tie yourself in knots :), then here's an example iterating thro a formcollection:

但是,如果你热衷于打结自己:),那么这里是一个迭代一个formcollection的例子:

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

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

#3


0  

Something like (code not tested) -

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

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

#4


0  

Create a view like this

创建这样的视图

<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>

Create action like below

创建如下所示的操作

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

Note: Above written code is not a recommended way of doing things, its just for a demo.

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

#5


0  

Please try this example,hope it will help ...

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

 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);
        }

Create two view - DisplayFormCollectionData FormCollectionEg

创建两个视图 - DisplayFormCollectionData FormCollectionEg

DisplayFormCollectionData

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-

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>

#6


0  

My preferred option is to use UpdateModel.

我首选的选择是使用UpdateModel。

Instead of manually mapping the fields, this MVC method will automatically bind the properties from the data available in the request, the same way as if you had passed a strict type in as a parameter to the action.

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

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

    UpdateModel<Username>(model);

    return View(model);
}

The above code will include data from the QueryString aswell as the form data which may not be suitable. If you were using a parameter in the action, you'd restrict this by using [FromBody], but with UpdateModel you can still achieve the same thing by passing in the FormCollection as the value provider.

上面的代码将包含来自QueryString的数据以及可能不合适的表单数据。如果您在操作中使用了参数,则可以使用[FromBody]来限制它,但是使用UpdateModel,您仍然可以通过传递FormCollection作为值提供程序来实现相同的功能。

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

    UpdateModel<Username>(model, collection);

    return View(model);
}

#1


24  

If you have:

如果你有:

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

You could use the attribute name of the element like below:

您可以使用元素的属性名称,如下所示:

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

#2


3  

I think you should attempt to stear clear of the formcollection object if you are able to, in favour of a strongly typed viewmodel. there are a few examples here on SO and i've linked the 1st one that I searched for:

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

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

通过JQuery Post方法将FormCollection传递给控制器​​并获取数据...

however, if you're keen to tie yourself in knots :), then here's an example iterating thro a formcollection:

但是,如果你热衷于打结自己:),那么这里是一个迭代一个formcollection的例子:

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

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

#3


0  

Something like (code not tested) -

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

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

#4


0  

Create a view like this

创建这样的视图

<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>

Create action like below

创建如下所示的操作

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

Note: Above written code is not a recommended way of doing things, its just for a demo.

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

#5


0  

Please try this example,hope it will help ...

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

 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);
        }

Create two view - DisplayFormCollectionData FormCollectionEg

创建两个视图 - DisplayFormCollectionData FormCollectionEg

DisplayFormCollectionData

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-

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>

#6


0  

My preferred option is to use UpdateModel.

我首选的选择是使用UpdateModel。

Instead of manually mapping the fields, this MVC method will automatically bind the properties from the data available in the request, the same way as if you had passed a strict type in as a parameter to the action.

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

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

    UpdateModel<Username>(model);

    return View(model);
}

The above code will include data from the QueryString aswell as the form data which may not be suitable. If you were using a parameter in the action, you'd restrict this by using [FromBody], but with UpdateModel you can still achieve the same thing by passing in the FormCollection as the value provider.

上面的代码将包含来自QueryString的数据以及可能不合适的表单数据。如果您在操作中使用了参数,则可以使用[FromBody]来限制它,但是使用UpdateModel,您仍然可以通过传递FormCollection作为值提供程序来实现相同的功能。

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

    UpdateModel<Username>(model, collection);

    return View(model);
}