1

I can send lat,lon, neighbors and neighborslimit variables to View.Yet, I want change neighborlimit from view. When I post View, MapViewModel's variables are 0, I have tried to ModelState.Clear() but there is no difference, Could you help me about it ? Thanks

MODEL:

public class MapViewModel
{
    public double lat;
    public double lon;
    public List<Point> neighbors;
    public Polygon polygon;

    public int neighborlimit;
    public double[][] polyTable;
}

CONTROLLER:

    [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); 
    }

VIEW:

@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>
}

2 Answers 2

1

You don't have a property for neighborlimit (just a field). Change it to

public int neighborlimit { get; set; }

which will allow the DefaultModelBinder to set the property when you submit the form

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works but I have another problem now, UserManager.Update(user) does not change value from database, I have taken an error, An exception of type 'System.Data.Entity.Validation.DbEntityValidationException' occurred in mscorlib.dll but was not handled in user code Do you have idea about it?
I have no idea what you UserManager.Update(user); function does. You will need to ask a new question with the details, including the error message
0

The problem is that you don't have the values in the form that's why when the form is posted the values doesn't exists and the ModelBinder set default values. If the security is not a problem but hidden fields for all values that you want to persist.

Something like this

@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>
}

UPDATE

As Stephen Muecke said make sure that you use properties not fields

2 Comments

I tried but values are still 0, When I post, values are 0 which are get to View, rest are Null, it makes sense? For example; lat=0 lon=0 neighborlimit=0 polygon=Null polyTable=Null
As @Stephen Muecke said you should change it from fields to properties. I didn't saw you are using fields not properties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.