asp.net mvc新问题来了

d383137359 2018-08-13 09:57:48
如题,有以下情况?何解


@model IEnumerable<Student>

@using(Ajax.BeginForm())
{
<ul>
@foreach(var item in Mode)
{
<li>
@Html.HiddenFor(m => item.id)
@Html.TextBoxFor(m => item.name)
</li>
}
</ul>
<input type="submit" value="提交" />
}



public ActionResult Index()
{
List<Student> list = new List<Student>();
for(int i=0; i<5; i++)
{
list.add(new Student(i, "学生"+i));
}
return View(list);
}

public ActionResult Index(string id, string name)
{
//此时id有值,name也有值。但是前台的textbox的值改变后,提交上来的值依然是原值,why??
return View();
}
...全文
174 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
正怒月神 版主 2018-08-13
  • 打赏
  • 举报
回复
引用 5 楼 d383137359 的回复:
[quote=引用 3 楼 hanjun0612 的回复:]
以为你是一个
@foreach(var item in Mode)
{
<li>
@Html.HiddenFor(m => item.id)
@Html.TextBoxFor(m => item.name)
</li>
}
所以后台应该用List<string> 接收

看一下最后的submit提交
https://blog.csdn.net/hanjun0612/article/details/51918059


我更改成这样子实验了一下,接收不到数

@{ int index = 0 ;}
@foreach(var item in Mode)
{
<li>
@Html.HiddenFor(m => item.id, new { Id = "item["+(index++)+"].id", Name="item["+(index++)+"].id"})
@Html.TextBoxFor(m => item.name, new {Id = "item["+(index++)+"].name", Name="item["+(index++)+"].name"})
</li>
}




public ActionResult Index(int id = 0, string name="") // name为空
public ActionResult Index(Student stu) // stu为null
public ActionResult Index(List<Student> list) //list为null
public ActionResult Index(List<string> listname) //listname为null
{
return View();
}
[/quote]

记得后台的参数名,和前台的Name名对应,应该是Name名称[下标].属性
给你简单写了个例子

d383137359 2018-08-13
  • 打赏
  • 举报
回复
引用 3 楼 hanjun0612 的回复:
以为你是一个
@foreach(var item in Mode)
{
<li>
@Html.HiddenFor(m => item.id)
@Html.TextBoxFor(m => item.name)
</li>
}
所以后台应该用List<string> 接收

看一下最后的submit提交
https://blog.csdn.net/hanjun0612/article/details/51918059


我更改成这样子实验了一下,接收不到数

@{ int index = 0 ;}
@foreach(var item in Mode)
{
<li>
@Html.HiddenFor(m => item.id, new { Id = "item["+(index++)+"].id", Name="item["+(index++)+"].id"})
@Html.TextBoxFor(m => item.name, new {Id = "item["+(index++)+"].name", Name="item["+(index++)+"].name"})
</li>
}




public ActionResult Index(int id = 0, string name="") // name为空
public ActionResult Index(Student stu) // stu为null
public ActionResult Index(List<Student> list) //list为null
public ActionResult Index(List<string> listname) //listname为null
{
return View();
}
d383137359 2018-08-13
  • 打赏
  • 举报
回复
引用 2 楼 weixin_38925245 的回复:
你这里不应该是个list吗。



引用 1 楼 HXXCCC 的回复:
你提交的是一个list吧。接收的是单个的id name?



public ActionResult Index(List<Student> list)
{
//这样的话,list将会是null
return View();
}
正怒月神 版主 2018-08-13
  • 打赏
  • 举报
回复
以为你是一个
@foreach(var item in Mode)
{
<li>
@Html.HiddenFor(m => item.id)
@Html.TextBoxFor(m => item.name)
</li>
}
所以后台应该用List<string> 接收

看一下最后的submit提交
https://blog.csdn.net/hanjun0612/article/details/51918059
极客诗人 2018-08-13
  • 打赏
  • 举报
回复
你这里不应该是个list吗。
HXXCCC 2018-08-13
  • 打赏
  • 举报
回复
你提交的是一个list吧。接收的是单个的id name?
正怒月神 版主 2018-08-13
  • 打赏
  • 举报
回复
引用 8 楼 d383137359 的回复:
[quote=引用 6 楼 hanjun0612 的回复:]
记得后台的参数名,和前台的Name名对应,应该是Name名称[下标].属性
给你简单写了个例子



该方法确实可行,能收到了,我忽略了一个问题,我的提交在循环里面
如果我在循环里使用
@Ajax.ActionLink("单行提交", "Test1" , new { name=item.Name, age=item.Age}, new AjaxOptions() { } )
这种情况,后台使用
public ActionResult Test1(string name="", int age=0)
这两个参数获取到的值是 修改前的,不能获取到修改后。这个是什么原因呢?[/quote]
不清楚,你看看action增加 [httpget]试试看
d383137359 2018-08-13
  • 打赏
  • 举报
回复
引用 6 楼 hanjun0612 的回复:
记得后台的参数名,和前台的Name名对应,应该是Name名称[下标].属性
给你简单写了个例子



该方法确实可行,能收到了,我忽略了一个问题,我的提交在循环里面
如果我在循环里使用
@Ajax.ActionLink("单行提交", "Test1" , new { name=item.Name, age=item.Age}, new AjaxOptions() { } )
这种情况,后台使用
public ActionResult Test1(string name="", int age=0)
这两个参数获取到的值是 修改前的,不能获取到修改后。这个是什么原因呢?
sdfgrtyu 2018-08-13
  • 打赏
  • 举报
回复
找本书看看模型绑定那一章吧,,,,

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧