MVC: pass parameter to view?

后端 未结 5 427
梦谈多话
梦谈多话 2020-12-14 19:52

MVC newbie question:

I\'m picking up a URL of the form go/{mainnav}/{subnav}, which I\'ve successfully routed to the GoController class, me

相关标签:
5条回答
  • 2020-12-14 20:12

    If you are using MVC3 I would suggest passing the values into the ViewBag.

    ViewBag.MainNav = "xxxx";
    ViewBag.SubNav = null;
    

    then on your view page, where you define the JavaScript and add the value.

    if you dont have MVC 3 if you use ViewData["MainNav"]), to store your value has the same effect.

    0 讨论(0)
  • 2020-12-14 20:13

    i'm using following approach:

    ViewModel.class:

    public class TitleBodyModel
    {
        public string Title { get; set; }
    
        public string Body { get; set; }
    
        public TitleBodyModel() { Title = Body = ""; }
    
        public TitleBodyModel(string t, string b) { this.Title = t; this.Body = b; }
    }
    

    In the main View:

    @Html.Partial("_TitleBody" , new XXX.Models.TitleBodyModel("title", "body" ))
    

    Then in a partial view:

    @model XXX.Models.TitleBodyModel
    
    
    <div class="bl_item">
        <div class="bl_title">@Model.Title</div>
    
        <div class="bl_body">@Model.Body</div>
    </div>
    
    0 讨论(0)
  • 2020-12-14 20:25

    Did you try accessing parameters from Request in your view?

    i.e.

    Request.Params["mainnav"]
    

    or

    Request.Params["subnav"]
    

    Works in MVC

    0 讨论(0)
  • 2020-12-14 20:31

    If i understand this can be a solution:

    public ActionResult Index(string mainnav, string subnav)
    {
       return View(mainnav | subnav);
    }
    

    In the Html View you can use View and after

    <%=Model %>
    
    0 讨论(0)
  • 2020-12-14 20:39

    You use a ViewModel class to transfer the data:

    public class IndexViewModel
    {
        public string MainNav { get; set; }
        public string SubNav { get; set; }
    
        public IndexViewModel(string mainnav, string subnav)
        {
            this.MainNav = mainnav;
            this.SubNav = subnav;
        }
    }
    

    Your action method then comes out

    public ActionResult Index(string mainnav, string subnav)
    {
        return View(new IndexViewModel(mainnav, subnav));
    }
    

    This means your view has to be strongly typed:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<YourNameSpace.IndexViewModel>" %>
    

    In your view, you output the data like so:

    myobj.mainnav = <%: Model.MainNav %>;
    

    An alternative solution if you use MVC3, would be to use the dynamic ViewBag:

    public ActionResult Index(string mainnav, string subnav)
    {
        ViewBag.MainNav = mainnav;
        ViewBag.SubNav = subnav;
        return View();
    }
    

    which would be accessed in the page as so:

    myobj.mainnav = <%: ViewBag.MainNav %>;
    

    However, I would recommend that you read up on unobtrusive javascript and see if you can improve your design to avoid this specific problem.

    0 讨论(0)
提交回复
热议问题