从部分视图MVC传递变量

时间:2015-02-04 18:58:39

标签: jquery asp.net-mvc asp.net-mvc-4

我正在创建一个包含从数据库填充(使用jQuery)的表的页面。我试图找出搜索表格的最佳方式(或者我猜正确的单词是"过滤")。 我有一个"局部视图" .cshtml文件,在表所在的文件中创建搜索栏。我的问题是如何从部分视图文件向"实际"传递变量(在按下搜索按钮时,文本框内唯一的内容)。查看文件,所以我可以用它作为过滤器的参数? 我在网上搜索过很多内容,无法找到我正在寻找的内容。 在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

<强> Jquery的

$(document).ready(function(){
    $.get("~/Student/GetStudent?id=1", function(html){
        //append html
    })
})

<强> C#

假设此get方法位于名为Student

的控制器中
[HttpGet]
public ActionResult GetStudent(int id)
{
    //use the paremeter in this case id to create a model with the correct data

    //_StudentPartial being the partial html and model being the model that populates the partial
    return PartialView("_StudentPartial",model);
}