Asp.net MVC 视图文本框 Returns 默认值
Asp.net MVC View Textbox Returns Default Value
我可以将 lat、lon、neighbors 和 neighborslimit 变量发送到 View.Yet,我想从视图中更改 neighborlimit。当我post查看时,MapViewModel的变量为0,我试过ModelState.Clear()
但没有区别,你能帮我看看吗?谢谢
型号:
public class MapViewModel
{
public double lat;
public double lon;
public List<Point> neighbors;
public Polygon polygon;
public int neighborlimit;
public double[][] polyTable;
}
控制器:
[HttpGet]
public ActionResult Map()
{
UserAccount user = (UserAccount)UserManager.FindByName(User.Identity.Name);
MapViewModel model = new MapViewModel() { lat = (double)user.address.latitude, lon = (double)user.address.longitude, neighbors = user.getNeighbors(), neighborlimit= (int)user.neighborsLimit };
return View(model);
}
[HttpPost]
public ActionResult Map(MapViewModel model)
{
UserAccount user = (UserAccount)UserManager.FindByName(User.Identity.Name);
user.neighborsLimit = model.neighborlimit;
UserManager.Update(user);
return View(model);
}
视图:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(h => h.neighborlimit, new { @class = "form-control" })
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
}
问题是您没有表单中的值,这就是为什么在发布表单时值不存在并且 ModelBinder 设置默认值的原因。如果安全性不是问题,而是要保留所有值的隐藏字段。
像这样
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(h => h.lat)
/* Now enter hidden fields for all of the properties that you want */
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(h => h.neighborlimit, new { @class = "form-control" })
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
}
更新
正如 Stephen Muecke 所说,确保您使用属性而不是字段
您没有 neighborlimit
的 属性(只有一个字段)。改成
public int neighborlimit { get; set; }
这将允许 DefaultModelBinder
在您提交表单时设置 属性
我可以将 lat、lon、neighbors 和 neighborslimit 变量发送到 View.Yet,我想从视图中更改 neighborlimit。当我post查看时,MapViewModel的变量为0,我试过ModelState.Clear()
但没有区别,你能帮我看看吗?谢谢
型号:
public class MapViewModel
{
public double lat;
public double lon;
public List<Point> neighbors;
public Polygon polygon;
public int neighborlimit;
public double[][] polyTable;
}
控制器:
[HttpGet]
public ActionResult Map()
{
UserAccount user = (UserAccount)UserManager.FindByName(User.Identity.Name);
MapViewModel model = new MapViewModel() { lat = (double)user.address.latitude, lon = (double)user.address.longitude, neighbors = user.getNeighbors(), neighborlimit= (int)user.neighborsLimit };
return View(model);
}
[HttpPost]
public ActionResult Map(MapViewModel model)
{
UserAccount user = (UserAccount)UserManager.FindByName(User.Identity.Name);
user.neighborsLimit = model.neighborlimit;
UserManager.Update(user);
return View(model);
}
视图:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(h => h.neighborlimit, new { @class = "form-control" })
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
}
问题是您没有表单中的值,这就是为什么在发布表单时值不存在并且 ModelBinder 设置默认值的原因。如果安全性不是问题,而是要保留所有值的隐藏字段。
像这样
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(h => h.lat)
/* Now enter hidden fields for all of the properties that you want */
<div class="form-group">
<div class="col-md-10">
@Html.TextBoxFor(h => h.neighborlimit, new { @class = "form-control" })
</div>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Log in" class="btn btn-default" />
</div>
</div>
}
更新
正如 Stephen Muecke 所说,确保您使用属性而不是字段
您没有 neighborlimit
的 属性(只有一个字段)。改成
public int neighborlimit { get; set; }
这将允许 DefaultModelBinder
在您提交表单时设置 属性