0
votes

I have a model class and a viewmodel class on the similar lines as model class (Code is given below) in a MVC application. There is a view whose model is 'viewmodel' and its a form. The corresponding action method for this form submit button has action parameter as viewmodel class. With this scenario mentioned, model binding works fine. Now if i change the data type of the Action method from "viewmodel" to "Model" class , Model binding doest not work accurately and correct data is not received on the server side. Below are the model classes -

public class Model
  {
     public int A { get; set; }
  }

public class ViewModel
    {
        public Model Model { get; set; }
    }

View File code is below :

@model ViewModel
@{
    ViewBag.Title = "Test";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Create</h2>

@using (Html.BeginForm("A", "Customers"))
{
    <div class="form-group">

        @Html.LabelFor(m => m.Model.A)
        @Html.TextBoxFor(m => m.Model.A, new { @class="form-control" })

    </div>

    <button type="submit" class="btn btn-primary">Save</button>
}

Controller Action Method : With Correct Model Binding behavior -

public ActionResult A(ViewModel model)
        {
            return Content("Value of A is " + model.Model.A); 
        }

If i change the action parameter to Model class , model binding behavior doesnot take place. Below is the code -

public ActionResult A(Model model)
        {
            return Content("Value of A is " + model.A); 
        }

Why is it so ?

On the browser side , the form data is as - Model.A - 1 Why cant this value bind to Model Class A parameter in the second case metioned ?

1
Because you generate name attributes based on ViewModel, not Model, therefore the parameter in the POST method must match (or you can use the [Bind(Prefix="Model")] attribute). As a side note, view models do not contain data models - they contain the properties of your data model that you need in the view - refer What is ViewModel in MVC? - user3559349
I might be misunderstanding something, but could you not just change it to @model Model? Is there a reason you want to nest it inside ViewModel? - velsietis
@velsietis - I want viewModel only in my view file. View model let add more properties and a complex class type can be used rather than just a model class. What i have presented in the question is just a main crust of the problem. In actual scenario, i have many other properties in my ViewModel class. - SunilA
@SunilA then it sounds like Bind Prefix (as suggested by Stephen Muecke) is what you need - velsietis
@StephenMuecke - It worked. - SunilA

1 Answers

1
votes

As your view is bound with ViewModel class (Model binding) so it carries the same model in the post event to server. but in case you wanted to bind with some other model then you have to form new model on the client side like your server side model.

In your case to achieve what you are looking for follow the below code:

 <input type="button" value="Save" onclick="SaveData()"/>

<script>
    function SaveData() {
        $.ajax({
            url: "/Home/Save",
            dataType: "json",
            type: "Post",
            data: {
               model:{A:10}
            },
            success:function(data) {

            }
        });
    }

</script>

Please keep the same variable name in client and server side.

public ActionResult Save(Model model)
    {
        return Content("Value of A is " + model.A);
    }