C# 使用MVC从包装的selectViewList将URL中的多个结果从视图传递回控制器
我目前正在用一个提交按钮填充视图中的两个下拉列表,该按钮将通过选择。但是,通过该方法访问时,url中的参数返回为null,尽管我可以看到它填充在字符串中: 预期URL: 但我得到的网址是: “提交”按钮时访问的控制器方法如下所示:C# 使用MVC从包装的selectViewList将URL中的多个结果从视图传递回控制器,c#,asp.net-mvc,C#,Asp.net Mvc,我目前正在用一个提交按钮填充视图中的两个下拉列表,该按钮将通过选择。但是,通过该方法访问时,url中的参数返回为null,尽管我可以看到它填充在字符串中: 预期URL: 但我得到的网址是: “提交”按钮时访问的控制器方法如下所示: public ActionResult DisplaySelection(string CatId, string BrandId) { ViewBag.catId = CatId; ViewBag.brandId
public ActionResult DisplaySelection(string CatId, string BrandId)
{
ViewBag.catId = CatId;
ViewBag.brandId = BrandId;
return View(ViewBag);
}
名称的更改是由于当我将下拉列表中的数据传递到视图时,因为我有两个数据列表,我有一个包装类,所以我必须选择正确的列表来显示,然后才能使用数据
例如:
public class SecondDropDownViewModel
{
public string secondId { get; set; }
public IEnumerable<SelectListItem> List { get; set; }
public static List<SelectListItem> BuildSecondList(IEnumerable<Dtos.Second> second)
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "item 1", Value = "itemd 1"
items.Add(new SelectListItem { Text = "item 2", Value = "itemd 2"
items.Add(new SelectListItem { Text = "item 3", Value = "itemd 3"
return items;
}
}
public class FirstDropDownViewModel
{
public string firstId { get; set; }
public IEnumerable<SelectListItem> List { get; set; }
public static List<SelectListItem> BuildFirstList(IEnumerable<Dtos.First> first)
{
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "item 1", Value = "itemd 1"
items.Add(new SelectListItem { Text = "item 2", Value = "itemd 2"
items.Add(new SelectListItem { Text = "item 3", Value = "itemd 3"
return items;
}
}
public class DropDownViewModel
{
public FirstDropDownViewModel firstList { get; set; }
public BrandDropDownViewModel secondist { get; set; }
}
视图:
@using (Html.BeginForm("DisplaySelection", "store", FormMethod.Get)){
<fieldset>
Grade Type
@Html.DropDownListFor(m => m.firstList.CatId, Model.firsList.List, "--Select Category--")
@Html.DropDownListFor(m => m.secondList.BrandId, Model.secondList.List, "--Select Brands--")
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
@使用(Html.BeginForm(“DisplaySelection”、“store”、FormMethod.Get)){
等级类型
@Html.DropDownListFor(m=>m.firstList.CatId,Model.firsList.List,“--选择类别--”)
@Html.DropDownListFor(m=>m.secondList.BrandId,Model.secondList.List,“--Select Brands-->”)
}
在接受表单输入的操作中,作为表单一部分生成的名称没有相应的属性可绑定。请尝试以下操作,将参数添加到DropDownListFor
以指定名称:
@Html.DropDownListFor(m => m.firstList.firstId, Model.firstList.List, "--Select Category--", new {Name= "CatId" })
@Html.DropDownListFor(m => m.secondist.secondId, Model.secondist.List, "--Select Brands--", new {Name= "BrandId" })
如果检查生成表单的HTML(按F12键),您将看到下拉列表的表单名称类似于object\u propertyname
。操作上的签名没有与该名称匹配的参数,也没有具有该名称属性的对象的参数。这两种方法都可以,就像更改上面的名称一样
也请考虑“<代码> POST <代码> >代码>获取<代码> >如果您正在编写的操作将导致服务器发生任何更改。
< P>作为表单的一部分生成的名称在您接受表单输入的动作中没有绑定到相应的属性。请尝试以下操作,将参数添加到DropDownListFor
以指定名称:
@Html.DropDownListFor(m => m.firstList.firstId, Model.firstList.List, "--Select Category--", new {Name= "CatId" })
@Html.DropDownListFor(m => m.secondist.secondId, Model.secondist.List, "--Select Brands--", new {Name= "BrandId" })
如果检查生成表单的HTML(按F12键),您将看到下拉列表的表单名称类似于object\u propertyname
。操作上的签名没有与该名称匹配的参数,也没有具有该名称属性的对象的参数。这两种方法都可以,就像更改上面的名称一样
也请考虑“<代码> POST <代码> > < <代码> >如果您正在编写的操作将导致服务器发生任何更改。< /P>正确生成您的视图模型,正确使用属性<代码>字符串CATID和String <代码> BLANDUD> <代码> >绑定和<代码> IEnumerable CatList < /代码>和<代码> IEnumerable BrandList < /代码>(除非你想搞糟你的应用程序,否则千万不要尝试更改名称属性)使用绑定到和
IEnumerable CatList
和IEnumerable BrandList
的属性string CatId
和stringBrandId
正确生成视图模型(除非你想搞糟你的应用程序,否则千万不要尝试更改name属性)