现在学习ASP MVC,仅仅是我在MVC上的第3周
我对建模传递做了一些测试,基本上控制器只是获取模型,并且没有做任何事情就进入视图,但似乎代码失败了。
下面的是我创建的ViewModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Bank2.Models.ViewModel
{
public class PaymentView
{
public List<Wires_SWIFT> lists{get; set;}
public string b_str{get; set;}
public string o_str{get; set;}
}
}
这是视图:
@model ViewModel
@using(Html.BeginForm("Payment","Home",FormMethod.Post)){
@Html.TextBoxFor(d=> d.o_str)<br/>
@Html.TextBoxFor(d=> d.b_str)<br/>
<input type="submit" name="Search">
}
控制器抓住模型并立即通过
...
[HttpPost]
public ActionResult Payment(ViewModel m){
return View(m)
}
...
我在texbox中键入了两个字符串:比如“aa”和“bb”,在我点击提交后,他们应该在那里,因为相同的对象被传回,但字段现在是空的
我是否遗漏了一些关于建模传递的重要内容?欢迎提出任何建议
答案 0 :(得分:5)
您需要为ViewModel
设置getter和setter才能从已发布的表单中检索值。
public class ViewModel
{
public string str1 { get; set; }
public string str2 { get; set; }
public List<int> list { get; set; }
}
答案 1 :(得分:0)
您正在向我们展示该方法的POST版本。通常,post action方法会采用模型,进行某种处理(输入验证,保存到db,调用服务等),然后如果成功,则重定向到不同的页面。
如果您对需要用户修复的输入有任何问题,通常只会从POST操作方法调用该视图。