我有一个模型对象结构,Foo其中包含一个Bar带有字符串值的类.
public class Foo
{
public Bar Bar;
}
public class Bar
{
public string Value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
以及使用这种结构的视图模型
public class HomeModel
{
public Foo Foo;
}
Run Code Online (Sandbox Code Playgroud)
然后,我有一个表格,在Razor看起来像这样.
<body>
<div>
@using (Html.BeginForm("Save", "Home", FormMethod.Post))
{
<fieldset>
@Html.TextBoxFor(m => m.Foo.Bar.Value)
<input type="submit" value="Send"/>
</fieldset>
}
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
在html中成为.
<form action="/Home/Save" method="post">
<fieldset>
<input id="Foo_Bar_Value" name="Foo.Bar.Value" type="text" value="Test">
<input type="submit" value="Send">
</fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)
最后控制器像这样处理post loos
[HttpPost]
public ActionResult Save(Foo foo)
{
// Magic happends here …Run Code Online (Sandbox Code Playgroud) 我已将文本框设置为只读.当用户单击它时,将显示日历,并且用户选择输入到只读文本框中的日期.
但是当我尝试将数据输入数据库时,它显示空值.怎么了?
我有以下POCO课程:
public class Location
{
public int LocationId { get; set; }
public string Name { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string ZipCode { get; set; }
public string Country { get; set; }
public float? Latitude { get; set; }
public float? Longitude { get; set; }
public string PhoneNumber { get; set; }
public string EmailAddress { get; …Run Code Online (Sandbox Code Playgroud) 我有一个禁用的输入字段,显示模型中的数据。
\n\n<div class="form-group">\n @Html.LabelFor(model => model.first_name, "F\xc3\xb6rnamn", new\n {\n @for = "inputFirstname",\n @class = "col-lg-3 control-label"\n })\n <div class="col-lg-9">\n @Html.TextBoxFor(model => model.first_name, new\n {\n @type = "text",\n @class = "form-control",\n @id = "inputFirstname",\n text = Html.DisplayNameFor(model => model.first_name),\n disabled="disabled"\n })\n </div>\n </div>\nRun Code Online (Sandbox Code Playgroud)\n\n我可以将此数据提交给 controlelr 方法:
\n\n[http邮报]
\n\npublic ActionResult Index(RegistrationModel RegistrationModelViewModel)\n{}\nRun Code Online (Sandbox Code Playgroud)\n\n当我添加disabled="disabled"first_name数据为空时,如果我删除它,我会得到正确的数据。
我究竟做错了什么?
\n