How do I access data from the model via the controller?

I know this seems like an easy question but I believe the viewbag is what gives me access to the data from a model but I want to access the model class directly and not create an instance of it. I know that you can do that with the main index method by using the model class as a parameter but how do I do the same thing with a method that I create?

Here is my code below and I'm trying to figure out how to pass the model class as a parameter so the method that I'm calling can access all of the information:

$(document).ready(function () {
        // if user changes paper type
        $('#paperTypeJList').change(function () {
        var value = $(this).val();

        $.ajax({
            url: '@Url.Action("getNewPrice","Home")',
            data: {dropdownValue:value, dropdownName: "paperType"},
            cache: false,
            async: true,
            success: function(response){
                alert(response);
            }
        });
    });

Upvotes: 0

Views: 97

Answers (1)

user3383479
user3383479

Reputation:

I have understood well your question. You can also call your method with like this

 [HttpPost]
 public JsonResult getNewPrice(EasyInfoModels model, string dropdownValue, string dropdownName )
{
    // do something with value and return a decimal
    if(string.IsNullOrEmpty(dropdownValue) && string.IsNullOrEmpty(dropdownName ))
     {
       //do something
       return Json(result)
     }
   else
    return Json("Blank");
}

and for your Ajax call

var datas = {
       fullName :$("#fullname").val(),//retrieve the value from your model in the view
       email:"",//retrieve the value from your model in the view
       phone :"",//retrieve the value from your model in the view
       topic:"",//retrieve the value from your model in the view
       subject:"",//retrieve the value from your model in the view
       paperType:"",//retrieve the value from your model in the view
       urgency :"",//retrieve the value from your model in the view
       numOfPages :"",//retrieve the value from your model in the view
       requirements :"",//retrieve the value from your model in the view
       style:"",//retrieve the value from your model in the view
       spacing :"",//retrieve the value from your model in the view
       price :'',//retrieve the value from your model in the view
       dropdownValue:value, 
       dropdownName: "paperType"          
    };
  $.ajax({
        url: '@Url.Action("getNewPrice","Home")',
        type:"POST",
        data:datas ,
        contentType: "application/json; charset=utf-8",
        cache: false,
        async: true,
        success: function(response){
            alert(response);
        }
    });

Hope it will help you. The Binder will take each value to create your model.

Upvotes: 1

Related Questions