使用部分视图将参数传递给控制器

时间:2012-10-25 23:57:49

标签: asp.net-mvc

我有一个PartialView已包含在其他两个视图中。

我如何能够将参数从PartialView传递到每个View的主控制器。

@Html.Partial("_Video","file1.xml")

@Html.Partial("_Video","file2.xml")

我希望能够在每个控制器上获取Get方法的参数值。

        //Controller1 
        // GET: //
        public ActionResult Index(){
             "file1.xml"
        }

        //Controller2
        // GET: //
        public ActionResult Index(){
             "file2.xml"
        }

1 个答案:

答案 0 :(得分:1)

我自己一直在解决这个问题,所以希望它可以帮助你。

在您的视图中使用cshtml:

@Html.Action("_Video", "Controller1", new { paramValue = "some string" } )

在这种情况下,您需要创建名为“_Video.cshtml”的部分视图(通过@model指令定义正确的模型类型)并在控制器中:

[ChildActionOnly]
public ActionResult _Video(String paramValue) {
   // paramValue == "some string"
   // .. do something with aModel ...
   return PartialView(aModel);
}

但是,如果您想要返回xml或类似的数据内容,请检查视图模型容器,Web Api或RESTful数据调用是否更适合(如果您正在调用,请搜索“Json()”函数也来自客户端脚本。)

HTH

相关问题