Partialview (return HTML (local))
When returning to view in ASP. Viewresult, it inherits from Viewresultbase and it has a brother Partialviewresult
I believe you know the difference between the two. One is used to return the whole, and the other returns the local (partial).
Let's say I have a requirement, enter a user name, and then return the relevant information. The previous practice might be to return the user's information in JSON format, and then go to the page to render the relevant HTML, if the resulting relative HTML is relatively large, I would recommend that you follow the previous scheme (return JSON), because the data transmitted less, faster response.
Conversely, Partialviewresult is a good choice for returning some HTML.
Now let's look at how to use the Partialviewresult:
Layout.cshtml
1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>@ViewBag. Title</title>5 <Scriptsrc= "@Url. Content ("~/scripts/jquery-1.4.4.min.js ")" Type= "Text/javascript"></Script>6 </Head>7 <Body>8 9 @RenderBody ()Ten One </Body> A </HTML>
Index.cshtml
@{viewbag.title = "Index"; Layout = "~/views/shared/_layout.cshtml";}<H2>Partialview Demo</H2><Div>Please write your name here<inputtype= ' text 'ID= ' txtname '/> <inputtype= ' button 'value= ' Submit 'ID= ' Btnok '/></Div><BR/><DivID= ' content '></Div><Scripttype= "Text/javascript"> $(function () { $('#btnOK'). Click (function () { varData={Name: $ ('#txtName'). Val ()}; $.ajax ({type:"POST", URL:'@Url. Action ("Partialviewdemo", "Home")', Data:data, datatype:"HTML", Success:function(data) {$ ('#content'). HTML (data); }, Error:function() {alert ("processing failed!"); } }); }); });</Script>
Viewusercontrol.cshtml (Partial View)
1 @model Sample.Models.PartialViewDemoViewModel2 3 <Div>4 5 <H2>Viewusercontrol.cshtml</H2> 6 7@Model. DT<BR/><BR/>8 9 hello~ @Model. NameTen One </Div>
or Viewusercontrol. ascx
1 <%@ Control Language="C #"Inherits="system.web.mvc.viewusercontrol<vancl.sample.models.partialviewdemoviewmodel>" %>2 3 <Div>4 <H2>Viewuc.ascx</H2> 5 6 <%=Model.dt%><BR/><BR/>7 8hello~<%=Model.name%>9 Ten </Div>
Model
1 public class Partialviewdemoviewmodel 2 { 4 5 public string Name {set ; get ;} 6 Public DateTime? DT {set ; get ;} 8 9 }
Action
1 [HttpPost] 2 Public ActionResult Partialviewdemo (Partialviewdemoviewmodel model) 3 {4 model.dt = DateTime.Now; 5 return Partialview ("viewusercontrol"67 // 8 }
Call the Controller. Partialview method , you can specify Partial view or view User Control The/c13> effect is the same
When you do not write a suffix, you will find files in the same directory and shared directory, that is, you can omit the suffix name when you are in the same directory or shared directory.
If there is a situation with the same name in the directory, the first one is found and returned.
eg: viewusercontrol.ascx and viewusercontrol.cshtml in the same catalogue
Use return partialview ("Viewusercontrol") at this time;
will return The contents of the Viewusercontrol.ascx because the letter A is before C :)
If you want to call viewusercontrol.cshtml in this case
You need to write the full path,return Partialview ("~/views/home/viewusercontrol.cshtml");
When you want to access the Partial view or view User Control in a different directory, you can also access it through a full-path approach.
Using Partialview to return partial HTML in ASP.