Asp.net mvc Asp.net MVC视图文本框返回默认值

Asp.net mvc Asp.net MVC视图文本框返回默认值,asp.net-mvc,razor,http-post,Asp.net Mvc,Razor,Http Post,我可以发送lat、lon、neighborslimit和neighborslimit变量来查看。但是,我想从视图中更改neighborlimit。当我发布视图时,MapViewModel的变量是0,我尝试了ModelState.Clear(),但是没有区别,你能帮我一下吗?谢谢 型号: public class MapViewModel { public double lat; public double lon; public List<Point> nei

我可以发送lat、lon、neighborslimit和neighborslimit变量来查看。但是,我想从视图中更改neighborlimit。当我发布视图时,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>
}
查看:

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>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@TextBoxFor(h=>h.neighborlimit,new{@class=“form control”})
}

问题在于表单中没有值,这就是为什么在发布表单时,值不存在,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>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@HiddenFor(h=>h.lat)
/*现在,为所需的所有属性输入隐藏字段*/
@TextBoxFor(h=>h.neighborlimit,new{@class=“form control”})
}
更新
正如Stephen Muecke(Stephen Muecke)所说的,确保使用属性而不是字段(只使用字段)。换成

public int neighborlimit { get; set; }

这将允许
DefaultModelBinder
在您提交表单时设置属性

我尝试过,但值仍然为0,当我发布时,值为0,可以查看,其余为空,这有意义吗?例如lat=0 lon=0 neighborlimit=0 polygon=Null polyTable=NullAs@Stephen Muecke说您应该将其从字段更改为属性。我没有看到您使用的是字段而不是属性hanks,它可以正常工作,但我现在有另一个问题,
UserManager.Update(user)
不会更改数据库中的值,我遇到了一个错误,mscorlib.dll中发生了类型为“System.Data.Entity.Validation.DbEntityValidationException”的异常,但未在用户代码中处理。您知道吗?我不知道您
UserManager.Update(用户)是什么函数执行。您需要提出一个新的问题,包括错误消息在内的详细信息