2014-04-14 65 views
0

我知道这看起来像一个简单的问题,但我相信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); 
      } 
     }); 
    }); 
+0

你能展示你的方法和你的模型吗? –

+0

我刚刚发布了代码 –

+0

你可以更新你的问题,你只需要更新'编辑'作为答案。如果我明白你想在你的模型中使用一种方法。但是不想实例化你的模型? –

回答

1

我有很好地理解你的问题。你也可以用这样的

[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将采取每个值来创建你的模型。

+0

时,这很奇怪,我想我没有很好地陈述我的问题。我试图通过调用方法的原始代码作为参数传递模型类。这是一个jQuery脚本,每次调用下拉列表中的值时,都会调用该方法 –

+0

我不明白代码如何帮助我从方法内部访问模型? –

相关问题