为什么模型绑定需要一个空的构造 [英] Why does the model binder need an empty constructor
问题描述
我需要一些帮助这里的一些基础知识...
我有这个控制器我的看法与类的一个实例提供了(至少这是我怎么想它的工作原理)。所以,因为我给我的看法对象的新实例,它为什么要创建一个新的一个型号为我的帖子结合回来?请看下面的例子。
[HTTPGET]
公众的ActionResult指数(){
INT HI = 5;
字符串TEMP =哟;
为MyModel富=新为MyModel(喜,温度);
返回查看(富);
}
[HttpPost]
公众的ActionResult指数(为MyModel富){
为MyModel便便= foo的;
如果(poo.someString == laaaa)
返回RedirctToAction(结束,EndCntrl便便);
其他
抛出新的异常();
}视图:
@model myApp.models.MyModel@ html.EditorFor(M = GT; m.hi)
<输入类型=提交值=打/>模型:
公共类为MyModel {
公众诠释喜{搞定;组;}
公共字符串someString {搞定;组;}
公众的东西(整型数字,字符串laaaa){
NumberforClass =号;
someString = laaaa;
}
}
为什么我需要一个空白的构造?此外,如果我有一个参数的构造函数,为什么会 poo.someString
按我到 RedirctToAction的时间(结束,EndCntrl转变,便便)
?
为什么我需要一个空白的构造?
块引用>因为
[HttpPost]
公众的ActionResult指数(为MyModel富){...}您要求的粘合剂给你上发表具体的实例,因此粘合剂需要创建对象为您服务。您的原始对象不GET和POST操作,只(部分),其特性住为HTML字段之间仍然存在。那是什么HTTP是无状态的意思。
当你使用较低的水平它变得更加略明显
[HttpPost]
公众的ActionResult指数(的FormCollection集合)
{
无功富=新为MyModel();
//从自己的FormCollection加载性能
}
为什么要由我到
RedirctToAction时间poo.someString变化(结束,EndCntrl便便)
?
块引用>由于
someString
未在视图中使用。所以,当你得到新的模式回到它永远是空白。要改变这种状况:@model myApp.models.MyModel
@ html.HiddenFor(M = GT; m.SomeString)@ html.EditorFor(M = GT; m.hi)
<输入类型=提交值=打/>这将值存储作为HTML隐藏字段,它会恢复对您的POST。
I need some help with some fundamentals here...
I have this controller that serves up my view with an instance of a class (at least that's how I think it works). So since I am giving my view a new instance of the object, why does it have to create a NEWer one for the model binding for my post back? Please look at the below example.
[HttpGet] public ActionResult Index(){ int hi = 5; string temp = "yo"; MyModel foo = new MyModel(hi, temp); return View(foo); } [HttpPost] public ActionResult Index(MyModel foo){ MyModel poo = foo; if(poo.someString == laaaa) return RedirctToAction("End", "EndCntrl", poo); else throw new Exception(); } View: @model myApp.models.MyModel @html.EditorFor(m => m.hi) <input type="submit" value="hit"/> Model: public class MyModel { public int hi {get; set;} public string someString {get; set;} public stuff(int number, string laaaa){ NumberforClass = number; someString = laaaa; } }
Why do I need a blank constructor? Furthermore, if I had an parameterless constructor, why would
poo.someString
change by the time I got toRedirctToAction("End", "EndCntrl", poo)
?解决方案Why do I need a blank constructor?
because of
[HttpPost] public ActionResult Index(MyModel foo){ ... }
You asked the binder to give you a concrete instance on Post, so the binder needs to create that object for you. Your original object does not persist between the GET and POST actions, only (some of) its properties live on as HTML fields. Thats what "HTTP is stateless" means.
It becomes a little more obvious when you use the lower level
[HttpPost] public ActionResult Index(FormCollection collection) { var Foo = new MyModel(); // load the properties from the FormCollection yourself }
why would poo.someString change by the time I got to
RedirctToAction("End", "EndCntrl", poo)
?Because
someString
isn't used in your View. So it will always be blank when you get the new model back. To change that:@model myApp.models.MyModel @html.HiddenFor(m => m.SomeString) @html.EditorFor(m => m.hi) <input type="submit" value="hit"/>
this will store the value as a hidden field in the HTML and it will be restored for you on POST.
这篇关于为什么模型绑定需要一个空的构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!