asp.net-mvc 字符串作为模型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16852537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String as a Model
提问by LastBye
I thought this should have been an easier task :
我认为这应该是一个更容易的任务:
Edit:
编辑:
It seems till this day Asp.Net MVC couldn't provide a neat solution on this case:
直到今天,Asp.Net MVC 似乎都无法在这种情况下提供简洁的解决方案:
If you want to pass a simple string as a model and you don't have to define more classes and stuff to do so...Any ideas ??
如果您想传递一个简单的字符串作为模型,并且您不必定义更多的类和东西来这样做......有任何想法吗??
here I'm trying to have a simple string model.
在这里,我试图有一个简单的字符串模型。
I'm getting this error :
我收到此错误:
"Value cannot be null or empty" / "Parameter name: name"
The View :
风景 :
@model string
@using (Html.BeginForm())
{
<span>Please Enter the code</span>
@Html.TextBoxFor(m => m) // Error Happens here
<button id="btnSubmit" title="Submit"></button>
}
The Controller :
控制器:
public string CodeText { get; set; }
public HomeController()
{
CodeText = "Please Enter MHM";
}
[HttpGet]
public ActionResult Index()
{
return View("Index", null, CodeText);
}
[HttpPost]
public ActionResult Index(string code)
{
bool result = false;
if (code == "MHM")
result = true;
return View();
}
采纳答案by Ant P
Eitherwrap the string in a view model object:
任一包装在一个视图模型对象的字符串:
Model:
模型:
public class HomeViewModel
{
public string CodeText { get; set; }
}
Controller:
控制器:
private HomeViewModel _model;
public HomeController()
{
_model = new HomeViewModel { CodeText = "My Text" };
}
[HttpGet]
public ActionResult Index()
{
return View("Index", _model);
}
View:
看法:
@Html.TextBoxFor(m => m.CodeText);
Oruse EditorForModel:
或使用EditorForModel:
@Html.EditorForModel()
回答by Pete
There's a much cleaner way of passing a string as a model into your view. You just need to use named parameters when returning your view:
有一种更简洁的方法可以将字符串作为模型传递到您的视图中。您只需要在返回视图时使用命名参数:
[HttpGet]
public ActionResult Index()
{
string myStringModel = "I am passing this string as a model in the view";
return View(model:myStringModel);
}
回答by Andras Zoltan
I know you've already accepted an answer here - I'm adding this because there's a general gotcha associated with using a string model.
我知道你已经在这里接受了一个答案 - 我添加这个是因为有一个与使用字符串模型相关的一般问题。
String as a model type in MVC is a nightmare, because if you do this in a controller:
String 作为 MVC 中的模型类型是一场噩梦,因为如果您在控制器中执行此操作:
string myStringModel = "Hello world";
return View("action", myStringModel);
It ends up choosing the wrong overload, and passing the myStringModel as a master name to the view engine.
它最终选择了错误的重载,并将 myStringModel 作为主名称传递给视图引擎。
In general it is easier simply to wrap it in a proper model type, as the accepted answer describes, but you can also simply force the compiler to choose the correct overload of View()by casting the string to object:
一般来说,如公认的答案所描述的那样,将它简单地包装在适当的模型类型中会更容易,但您也可以简单地强制编译器View()通过将字符串强制转换为 来选择正确的重载object:
return View("action", (object)myStringModel);
The other issue you're having here of using TextBoxForhaving issues with an 'unnamed' model - well you shouldn't be surprised by that... The only reason to use TextBoxForis to ensure the fields are named correctly for binding when the underlying value is a property on a model type. In this case there is no name, because you're passing it as a top-level model type for a view - so you it could be argued that you shouldn't be using TextBoxFor()in the first place.
您在这里遇到的另一个问题是使用TextBoxFor“未命名”模型有问题 - 好吧,您不应该对此感到惊讶......使用的唯一原因TextBoxFor是确保正确命名字段以进行绑定时基础值是模型类型的属性。在这种情况下,没有名称,因为您将它作为视图的顶级模型类型传递 - 因此您可能会争辩说您不应该首先使用TextBoxFor()它。

