[MVC][02]Where are user control, ajax components in MVC – Partial View

[MVC][02]Where are user control, ajax components in MVC - Partial View


Someone like me comes from asp.net web forms to MVC often wonders where is user control, ajax, footer, header components in MVC. For future convenient reference, here is going to show some simple and easy to be use examples:

1.Ajax:
first add a new cshtml:_ActorsTop2Data.cshtml, to the folder "/Views/Actors", the file name starts with underline means this is a partial view which will be used by ajax in "any other views, ex:Index.cshtml, Edit.cshtml, ....."


@model IEnumerable
    data returned from ajax:


    @foreach (var item in Model)
    {
        
    }
@Html.DisplayNameFor(model => model.FirstName) @Html.DisplayNameFor(model => model.LastName) @Html.DisplayNameFor(model => model.Title)
@Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) @Html.DisplayFor(modelItem => item.Title)


add this method to Controller "ActorsController.cs", any ajax call should come from Controller class:

//"NoStore = true, Duration = 0" to prevent server to get cached data
[OutputCache(NoStore = true, Duration = 0)]
public ActionResult ActorsTop2Data(string firstName, string lastName)
{
	using (MyDBEntities db = new MyDBEntities())
	{
		//ToList() is necessary, 
		//because using (MyDBEntities db = new MyDBEntities())
		//will automatically dispose 
		var top2Data = db.Actor.Where(ac=>ac.FirstName == firstName && ac.LastName == lastName)
			.Take(2).ToList();
		if (Request.IsAjaxRequest())
		{
			return PartialView("_ActorsTop2Data", top2Data);
		}
		else
		{
			return Content("Fail!");
		}

	}

}


Use this ajax partial view in Index.cshtml:
add following code at the end of Index.cshtml, do not forget to check the comment in code

@Ajax.ActionLink("Ajax.ActionLink passes 2 params and shows top 2 data in Actors", "ActorsTop2Data", new { firstName = "Chris", lastName = "Hemsworth" }, new AjaxOptions { UpdateTargetId = "ActorsTop2Data", InsertionMode = InsertionMode.Replace, HttpMethod = "POST" })

execute the project, and click the link at the end of page to get ajax result:


2. UserControl
how to use usercontrol in MVC is 95% similiar as ajax

add a new cshtml:_ActorsUserControl.cshtml, to the folder "/Views/Actors", the file name starts with underline means this is a partial view which will be used by usercontrol in "any other views, ex:Index.cshtml, Edit.cshtml, ....."


@model IEnumerable
    data returned from usercontrol:


    @foreach (var item in Model)
    {
        
    }
@Html.DisplayNameFor(model => model.FirstName) @Html.DisplayNameFor(model => model.LastName) @Html.DisplayNameFor(model => model.Title)
@Html.DisplayFor(modelItem => item.FirstName) @Html.DisplayFor(modelItem => item.LastName) @Html.DisplayFor(modelItem => item.Title)


add this method to Controller "ActorsController.cs", the data for usercontrol comes from here:

public ActionResult ActorsUserControlMethod(string keyWord)
{
	using (MyDBEntities db = new MyDBEntities())
	{
		//ToList() is necessary, 
		//because using (MyDBEntities db = new MyDBEntities())
		//will automatically dispose 
		var relatedData = db.Actor.Where(emp => emp.LastName.Contains(keyWord)).ToList();
		return PartialView("_ActorsUserControl", relatedData);

	}

}


​Use this usercontrol partial view in Index.cshtml:
add following code at the end of Index.cshtml, do not forget to check the comment in code

Html.Action show the top 2 data with keyword "H" in Actors: @Html.Action("ActorsUserControlMethod", new { keyWord = "H" })


execute the project, and the data of usercontrol will show automatically:



Project Direct Download Link

Reference:
How can I pass parameters to an Action using Html.Action() in ASP.NET MVC?
https://stackoverflow.com/questions/3150491/how-can-i-pass-parameters-to-an-action-using-html-action-in-asp-net-mvc
[ASP.NET MVC]部分检视详解(partial,renderpartial,action,renderAction,renderPage...)
https://dotblogs.com.tw/kevinya/2015/11/13/153909
[ASP.NET MVC]如何使用PARTIAL VIEW(部分检视)
https://dotblogs.com.tw/kevinya/2015/11/06/153816