在控制器中接受参数或原始数据? [英] Accepting params or raw data in controller?

查看:71
本文介绍了在控制器中接受参数或原始数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能在控制器函数中使用"params"自变量,或者类似的东西可以让我处理X形式的条目.

I was wondering if it would be possible having a "params" argument in a controller function, or something similar which would allow me to process X amount of entries in my form.

例如,我有一个表单,其中包含X个名称"元素,这些元素是通过jQuery自动生成的.这些名称元素的示例如下:

For instance, I have a form which has X amount of "name" elements, which are auto-generated through jQuery. An example of these name elements could be the following:

<input type="text" name="studentName1"></input>
<input type="text" name="studentName2"></input>
<input type="text" name="studentName3"></input>

现在,每次都有不同数量的学生姓名,因此这使我在控制器中处理表单数据变得相当复杂.我想到的是以下两个示例,但它们在现实中当然行不通.

Now, there's a different amount of student names every time, so this makes it quite complex for me to handle the form data in my controller. I had something like the following 2 examples in mind, but of course they wouldn't work in reality.

[HttpPost]
public ActionResult PostStudentNames(params string[] studentNames)

或者:

[HttpPost]
public ActionResult PostStudentNames(string[] formValues)

我可以达到类似的目的吗?

Can I achieve something similar to that?

推荐答案

我只是想用一种可用于此目的的不同方法来说明问题.如果更方便,则可以将模型绑定直接绑定到原始或复杂类型的集合.这里有两个例子:

I just want to chime in with a different approach you can use for this. If it's more convenient, you can model bind directly to collections of primitive or complex types. Here's 2 examples:

index.cshtml:

index.cshtml:

@using (Html.BeginForm("ListStrings", "Home"))
{
    <p>Bind a collection of strings:</p>

    <input type="text" name="[0]" value="The quick" /><br />
    <input type="text" name="[1]" value="brown fox" /><br />
    <input type="text" name="[2]" value="jumped over" /><br />
    <input type="text" name="[3]" value="the donkey" /><br />

    <input type="submit" value="List" />
}

@using (Html.BeginForm("ListComplexModel", "Home"))
{
    <p>Bind a collection of complex models:</p>

    <input type="text" name="[0].Id" value="1" /><br />
    <input type="text" name="[0].Name" value="Bob" /><br />
    <input type="text" name="[1].Id" value="2" /><br />
    <input type="text" name="[1].Name" value="Jane" /><br />

    <input type="submit" value="List" />
}

Student.cs:

Student.cs:

public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
}

HomeController.cs:

HomeController.cs:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult ListStrings(List<string> items)
    {
        return View(items);
    }

    public ActionResult ListComplexModel(List<Student> items)
    {
        return View(items);
    }
}

ListStrings.cshtml:

ListStrings.cshtml:

@foreach (var item in Model)
{
    <p>@item</p>
}

ListComplexModel.cshtml:

ListComplexModel.cshtml:

@foreach (var item in Model)
{
    <p>@item.Id. @item.Name</p>
}

第一种形式只是绑定字符串列表.第二个将表格数据绑定到List<Student>.通过使用这种方法,您可以让默认的模型绑定器为您完成一些繁琐的工作.

The first form simply binds a list of strings. The second, binds the form data to a List<Student>. By using this approach, you can let the default model binder do some of the tedious work for you.

是的,您也可以这样做:

Yes you can do that too:

表格:

@using (Html.BeginForm("ListComplexModel", "Home"))
{
    <p>Bind a collection of complex models:</p>

    <input type="text" name="[0].Id" value="1" /><br />
    <input type="text" name="[0].Name" value="Bob" /><br />
    <input type="text" name="[1].Id" value="2" /><br />
    <input type="text" name="[1].Name" value="Jane" /><br />
    <input type="text" name="ClassId" value="13" /><br />

    <input type="submit" value="List" />
}

控制器动作:

public ActionResult ListComplexModel(List<Student> items, int ClassId)
{
    // do stuff
}

这篇关于在控制器中接受参数或原始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆