如何传递参数的局部视图在MVC 4 [英] How can I pass parameters to a partial view in mvc 4

查看:389
本文介绍了如何传递参数的局部视图在MVC 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个链接:

 < A HREF ='会员/ MemberHome /资料/编号'><跨度>简介< / SPAN>< / A>

当我点击这个它会调用这个局部页面:

  @ {
    开关((字符串)ViewBag.Details)
    {        案简介:
        {
           @ Html.Partial(_个人资料);打破;
        }    }
}

部分页_Profile包括:

  Html.Action(行动,控制器,model.Paramter)

例如:

  @ Html.Action(MemberProfile,会员,新的{n = 1})// ID总是在变化

我的疑问是,我怎么能通过这个身份证到model.parameter部分

我的控制器:

 公众的ActionResult MemberHome(字符串ID)
    {
        ViewBag.Details = ID;
        返回查看();
    }
  公共的ActionResult MemberProfile(中间体编号= 0)
    {
        MemberData MD =新会员()GetMemberProfile(ID)。
        返回PartialView(_ ProfilePage,MD);
    }


解决方案

您的问题是很难理解的,但如果我得到的要点,你只需在你的主要观点,即要在部分访问某些价值所呈现的这一观点。

如果你只是渲染的部分只有部分名称:

  @ Html.Partial(_ SomePartial)

这实际上将通过模型作为一个隐含参数,一样的,如果你要拨打:

  @ Html.Partial(_ SomePartial模型)

现在,为了让您的部分居然能利用这一点,虽然,它也需要有一个定义的模型,例如:

  @model Namespace.To.Your.Model@ Html.Action(MemberProfile,会员,新{ID = Model.Id})

另外,如果你处理,这不是对您的视图模型的值(它在ViewBag或视图本身以某种方式产生的价值,那么你可以通过一个的ViewDataDictionary

  @ Html.Partial(_ SomePartial,新的ViewDataDictionary {{ID,someInteger}});

和则:

  @ Html.Action(MemberProfile,会员,新{ID =计算机[ID]})

与模型,剃刀将隐含通过你的局部视图的的ViewData 默认情况下,所以如果你有 ViewBag.Id 在你的观点,那么你可以参考同样的事情在你的部分。

I have a link like this:

 <a href='Member/MemberHome/Profile/Id'><span>Profile</span></a>

and when I click on this it will call this partial page:

 @{
    switch ((string)ViewBag.Details)
    {

        case "Profile":
        {
           @Html.Partial("_Profile"); break;
        }

    }
}

Partial page _Profile contains:

Html.Action("Action", "Controller", model.Paramter) 

Example:

@Html.Action("MemberProfile", "Member", new { id=1 })   // id is always changing

My doubt is that how can I pass this "Id" to model.parameter part?

My controllers are:

 public ActionResult MemberHome(string id)
    {
        ViewBag.Details = id;
        return View();
    }
  public ActionResult MemberProfile(int id = 0)
    {
        MemberData md = new Member().GetMemberProfile(id);
        return PartialView("_ProfilePage",md);
    }

解决方案

Your question is hard to understand, but if I'm getting the gist, you simply have some value in your main view that you want to access in a partial being rendered in that view.

If you just render a partial with just the partial name:

@Html.Partial("_SomePartial")

It will actually pass your model as an implicit parameter, the same as if you were to call:

@Html.Partial("_SomePartial", Model)

Now, in order for your partial to actually be able to use this, though, it too needs to have a defined model, for example:

@model Namespace.To.Your.Model

@Html.Action("MemberProfile", "Member", new { id = Model.Id })

Alternatively, if you're dealing with a value that's not on your view's model (it's in the ViewBag or a value generated in the view itself somehow, then you can pass a ViewDataDictionary

@Html.Partial("_SomePartial", new ViewDataDictionary { { "id", someInteger } });

And then:

@Html.Action("MemberProfile", "Member", new { id = ViewData["id"] })

As with the model, Razor will implicitly pass your partial the view's ViewData by default, so if you had ViewBag.Id in your view, then you can reference the same thing in your partial.

这篇关于如何传递参数的局部视图在MVC 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆