【发布时间】:2023-04-08 19:19:01
【问题描述】:
我在这里需要一些基础知识方面的帮助...
我有这个控制器,它为我的视图提供一个类的实例(至少我认为它是这样工作的)。所以,既然我给我的视图一个新的对象实例,为什么它必须为我的回发的模型绑定创建一个新的实例?请看下面的例子。
[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;
}
}
为什么我需要一个空白的构造函数?此外,如果我有一个无参数构造函数,为什么在我到达RedirctToAction("End", "EndCntrl", poo) 时poo.someString 会发生变化?
【问题讨论】:
-
阅读本文,了解模型绑定如何工作的基础知识。 msdn.microsoft.com/en-us/magazine/hh781022.aspx
-
你在说什么空构造函数?我在您的示例中看到的唯一构造函数是“stuff(int number, string laaaa)”
-
请使您的代码保持一致,仅发布实际编译的内容。
class stuff != class MyModel,hi != NumberforClass
标签: c# asp.net-mvc