Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
4.33/5 (2 votes)
See more:
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:

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


and i am returning users list by using a storeproc

C#
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
C#
public ActionResult Applications()
        {
            const string strApplicationCode = "APP";
            int userid = Convert.ToInt32(HttpContext.Current.Session["sysUserId"]);
            var applist = GetAvailableItems(userid, strApplicationCode);

            return PartialView(applist);
        }


And partial view Items.cshtml
C#
@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

C#
@{
    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>


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

ThaNK YOU
Posted
Updated 25-Apr-17 16:01pm

1 solution

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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900