Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 接受控制器中的参数或原始数据?_Asp.net Mvc_Asp.net Mvc 3 - Fatal编程技术网

Asp.net mvc 接受控制器中的参数或原始数据?

Asp.net mvc 接受控制器中的参数或原始数据?,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,我想知道是否可能在控制器函数中有一个“params”参数,或者类似的东西,允许我处理表单中的X个条目 例如,我有一个表单,其中包含X个“name”元素,这些元素是通过jQuery自动生成的。这些名称元素的示例如下: <input type="text" name="studentName1"></input> <input type="text" name="studentName2"></input> <input type="text"

我想知道是否可能在控制器函数中有一个“params”参数,或者类似的东西,允许我处理表单中的X个条目

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

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

我能实现类似的目标吗?

Mathias

这在不使用params对象的情况下工作得非常好。您的表单控件:

<input type="text" name="studentName" />
<input type="text" name="studentName" />
<input type="text" name="studentName" />
<input type="text" name="professorName" />

等等。。。至少,这是我对最近一个项目的回忆

我只是想插一句,你可以用另一种方法来解决这个问题。如果更方便的话,您可以直接将模型绑定到基本类型或复杂类型的集合。这里有两个例子:

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" />
}
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);
    }
}
公共类HomeController:控制器
{
公共行动结果索引()
{
返回视图();
}
公共操作结果列表字符串(列表项)
{
返回视图(项目);
}
public ActionResult ListComplexModel(列表项)
{
返回视图(项目);
}
}
ListStrings.cshtml:

@foreach (var item in Model)
{
    <p>@item</p>
}
@foreach(模型中的变量项)
{
@项目

}
ListComplexModel.cshtml:

@foreach (var item in Model)
{
    <p>@item.Id. @item.Name</p>
}
@foreach(模型中的变量项)
{
@item.Id.@item.Name

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

更新以供评论 是的,你也可以这样做:

表格:

使用(Html.BeginForm(“ListComplexModel”、“Home”)) { 绑定复杂模型的集合:






} 控制器操作:

public ActionResult ListComplexModel(List<Student> items, int ClassId)
{
    // do stuff
}
public ActionResult ListComplexModel(列表项,int-ClassId)
{
//做事
}

是的-这些天来,每当我必须打开一个webforms项目进行维护时,我几乎都会“呕吐”。mvc是如此的“正确”(对我来说——我知道这是一个品味和风格的问题),如果studentName输入的值有“,”的话,那么发生了什么事呢。检查不可靠的逗号当然是一个有先见之明的目标。好的捕获-可以使用
var-valueStudents=formValues[“studentName”].Split(',).Where(x=>x.Length>0.ToArray()当然。会更新答案吗?我能不能把它和一个额外的参数结合起来,我称之为“int ClassId”,它也来自于表单?@MathiasLykkegaardLorenzen Yep,更新的答案显示了一个例子。顺便说一句,如果你想知道如何使用这些数据的显示模板,菲尔·哈克(Phil Haack)将介绍整个过程。@MathiasLykkegaardLorenzen你能给我一个例子来说明你的意思吗?没关系,我已经把那部分修好了。现在我面临一个不同的问题。我的学生有一个双精度的“点”值。但是,当我启动表单时,由于服务器的本地化设置,表单无法工作。它需要一个“.”小数分隔符而不是“.”小数分隔符。我该怎么办?@MathiasLykkegaardLorenzen这是一个单独但常见的问题。查看Phil Haack关于该主题的文章,以及其他观点。
@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" />
}
public class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
}
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);
    }
}
@foreach (var item in Model)
{
    <p>@item</p>
}
@foreach (var item in Model)
{
    <p>@item.Id. @item.Name</p>
}
@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
}