如何通过控制器访问模型中的数据

本文关键字:数据 模型 访问 何通过 控制器 | 更新日期: 2023-09-27 17:58:22

我知道这似乎是一个简单的问题,但我相信viewbag让我可以访问模型中的数据,但我想直接访问模型类,而不是创建它的实例?

下面是我的代码,我正试图弄清楚如何将模型类作为参数传递,这样我调用的方法就可以访问所有信息:

$(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);
            }
        });
    });

如何通过控制器访问模型中的数据

我很理解你的问题。你也可以用这样的调用你的方法

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

对于您的Ajax调用

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

希望它能帮助你。Binder将使用每个值来创建您的模型。