MVC渲染部分视图与控制器的操作 [英] MVC Render Partial view with action from controller

查看:110
本文介绍了MVC渲染部分视图与控制器的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

i是MVC的新手,有人可以告诉我在视图中渲染局部视图时如何使用控制器中的操作。



例如,我有一个模型用户:



Hello,
i am new to MVC,Can someone tell me how to use an action from controller when rendering a partial view in a view.

for example,i have a model Users:

public class items
    {
        public string itemname{ get; set; }
        public int itemid{ get; set; }
       
    }





我将使用storeproc返回用户列表





and i am returning users list by using a storeproc

public static List<items> GetAvailableItems(int userid, string applicationcode)
{
          var dbcontext = new MasterDataContext();
          var applications = dbcontext.SPGeTItemList(userid
                                     , applicationcode).ToList();
          var appList = new List<Items>();
            if (applications.Any())
            {
                appList.AddRange(applications.Select(apps => new Items() 
                                    { appName =   apps.APPLICATION_CODE }));
            }
         return appList;
}





在我的控制器中我在我的控制器中有动作



in my controller i have an action in my controller

public ActionResult Applications()
        {
            const string strApplicationCode = "APP";
            int userid = Convert.ToInt32(HttpContext.Current.Session["sysUserId"]);
            var applist = GetAvailableItems(userid, strApplicationCode);

            return PartialView(applist);
        }





和局部视图Items.cshtml



And partial view Items.cshtml

@model Items

@{
    ViewBag.Title = "Items";
}

<h2>Items</h2>
<table>
    <tr>
        <th>
            itemname
        </th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.itemname)
           
        </td>
        
    </tr>
}

</table>





我在主视图中看到这个局部视图





and i am rendring this partial view in my main view

@{
    ViewBag.Title = "Home";
}
<div class="rdPanel">
    @Html.DevExpress().RoundPanel(
            settings =>
            {
                settings.Name = "rpFeatures";
                settings.HeaderText = "Available Items";
                settings.Width = 800;
                settings.SetContent(() =>
                {
                    Html.RenderPartial("Items");
                } );
            }).GetHtml()
</div>





但是如何使用我的操作从db获取列表。



ThaNK你



But how can i use my action to get the list from db.

ThaNK YOU

推荐答案

如果你想调用一个返回局部视图的动作结果你可以使用Html.RenderAction()



http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action- in-MVC-Razor.html [ ^ ]
If you want to call a action result which return a partial view you can use Html.RenderAction()

http://www.dotnet-tricks.com/Tutorial/mvc/Q8V2130113-RenderPartial-vs-RenderAction-vs-Partial-vs-Action-in-MVC-Razor.html[^]


这篇关于MVC渲染部分视图与控制器的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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