模型绑定在MVC中列出 [英] Model binding to list in MVC

查看:147
本文介绍了模型绑定在MVC中列出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能够取回在服务器端一个简单的列表。任何人都可以请点我在正确的方向?

 公共类TestList
{
    公共字符串ID {搞定;组; }
    公共字符串名称{;组; }
    公共字符串位置{搞定;组; }
}

形式:

  @model名单< SampleMVC4App.Controllers.TestList>
@ {
    ViewBag.Title =指数;
}
< H2>
    指数< / H>
@using(Html.BeginForm())
{
    <输入名称=卡斯特值=1型=隐藏/>
    <输入名称=[1] .IDVALUE =de107502-284d-459B-80a1-762ce0860cd8型=隐藏/>
    <输入名称=[1]。名称VALUE =测试1型=隐藏/>
    <输入名称=[1] .locationVALUE =LOCATION1类型=隐藏/>
    <一个ID =AddAnother的href =#>添加< / A>
    <输入类型=提交值=提交/>
}

控制器:

  [HttpPost]
公众的ActionResult编辑(ICollection的< TestList>卡斯特)** LT; ---空**
{
   返回查看();
}


解决方案

有关小时的工作后,我设法通过改变以下

去解决问题

 <输入名称=卡斯特值=1型=隐藏/>

 <输入名称=索引值=1型=隐藏/>

I am not able to retrieve a simple list on server side. Could anyone please point me in the right direction?

public class TestList
{
    public string id { get; set; }
    public string name { get; set; }
    public string location { get; set; }
}

Form:

@model List<SampleMVC4App.Controllers.TestList>
@{
    ViewBag.Title = "Index";
}
<h2>
    Index</h2>
@using (Html.BeginForm())
{           
    <input name="cust" value="1" type="hidden" />
    <input name="[1].id" value="de107502-284d-459b-80a1-762ce0860cd8" type="hidden" />    
    <input name="[1].name" value="test1" type="hidden" />    
    <input name="[1].location" value="location1" type="hidden" />    
    <a id="AddAnother" href="#">Add</a>
    <input type="submit" value="submit" />
}

Controller:

[HttpPost]
public ActionResult Edit(ICollection<TestList> cust) **<---Null**
{
   return View();
}

解决方案

After working for hours, I managed to work it out by changing below

<input name="cust" value="1" type="hidden" />

to

<input name="Index" value="1" type="hidden" />

这篇关于模型绑定在MVC中列出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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