Next, we add a view page through the Controller:
1. Add a new entry to the Shared directory in the view directory. The MVC view master page is as follows:
2. After the adding is complete, the following code is displayed:
3. open the HomeController class in the Controller directory, right-click the Index method, add a view, select "strong type", and add the content to List <MvcApplication16.Models. users>, select the dashboard page as the newly added ViewwMasterPage. master page. Generate the following code:
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ /Views/Shared/ViewMasterPage. Master "Inherits =" System. Web. Mvc. ViewPage <List <MvcApplication16.Models. Users> "%>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "TitleContent" runat = "server">
Index
</Asp: Content>
<Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
<Script type = "text/javascript">
Var I = 0;
Function MyVote (id ){
$. Get ("Vote. ashx? I = "+ I, {id: id}, function (data ){
If (data! = "0 "){
$ ("# A" + id).html (data );
Alert ("Vote successful! ");
} Else {
Alert ("failed to vote! ");
}
I ++;
});
}
</Script>
<H2> Index
<A href = "/Admin/Index/"> vote management </a>
<Table bgcolor = "# 3333ff" cellpadding = "1" cellspacing = "1" width = "95%" align = "center">
<Tr>
<% Foreach (var v in Model)
{%>
<Td align = "center" bgcolor = "white">
<br/>
Name: <% = v. userName %> Number of votes: <span id = "a <% = v. id %> "> <% = v. voteCount %> </span> <br/>
<Input type = "button" id = "tp" onclick = "MyVote (<% = v. id %>)" value = "Vote"/>
</Td>
<% }%>
</Tr>
</Table>
</Asp: Content>
4. Add a View to the Index method of the AdminController and specify the MvcApplication16.Models. Users type. The generated code is as follows:
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ /Views/Shared/ViewMasterPage. Master "Inherits =" System. Web. Mvc. ViewPage <MvcApplication16.Models. Users> "%>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "TitleContent" runat = "server">
Index
</Asp: Content>
<Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
<H2> Add the user to vote:
<% List <MvcApplication16.Models. Users> list = (List <MvcApplication16.Models. Users>) View. List; // obtain the list set and convert it to the List <Users> type.
%>
<Table bgcolor = "# 3333ff" cellpadding = "1" cellspacing = "1" width = "95%" align = "center">
<Tr> <th> User Name </th> <th> Number of votes </th> <th> Avatar </th> <th> operation </th> </tr>
<% Foreach (var v in list)
{%>
<Tr> <td bgcolor = "white"> <% = v. userName %> </td> <td bgcolor = "white"> <% = v. voteCount %> </td> <td bgcolor = "white"> <% = v. userPicPath %> </td> <td bgcolor = "white"> <a href = "/Admin/Delete /? Id = <% = v. id %> "> Delete </a> <a href =" "> modify </a> </td> </tr>
<% }%>
</Table>
<% Using (Html. BeginForm ("Create", "Admin", FormMethod. Post, new {enctype = "multipart/form-data "}))
{%>
<Table bgcolor = "# 3333ff" cellpadding = "1" cellspacing = "1" width = "95%" align = "center">
<Tr height = "40"> <td bgcolor = "white"> User name: </td> <td bgcolor = "white"> <% = Html. textBoxFor (m => m. userName) %> </td> </tr>
<Tr height = "40"> <td bgcolor = "white"> Avatar: </td> <td bgcolor = "white"> <input type = "file" name = "up"/> </td> </tr>
<Tr height = "40"> <td bgcolor = "white"> Number of votes: </td> <td bgcolor = "white"> <% = Html. textBoxFor (m => m. voteCount) %> </td> </tr>
<Tr height = "40"> <td bgcolor = "white"> operation: </td> <td bgcolor = "white"> <input type = "submit" value = "Add User"/> </td> </tr>
</Table>
<% }%>
</Asp: Content>
6. Add a view for the Edit (GET) method in the AdminController and specify the MvcApplication16.Models. Users type. The Code is as follows:
<% @ Page Title = "" Language = "C #" MasterPageFile = "~ /Views/Shared/ViewMasterPage. Master "Inherits =" System. Web. Mvc. ViewPage <MvcApplication16.Models. Users> "%>
<Asp: Content ID = "Content1" ContentPlaceHolderID = "TitleContent" runat = "server">
Edit
</Asp: Content>
<Asp: Content ID = "Content2" ContentPlaceHolderID = "MainContent" runat = "server">
<H2> Edit
<% Using (Html. BeginForm ("Edit", "Admin", FormMethod. Post, new {enctype = "multipart/form-data "}))
{%>
<Table bgcolor = "# 3333ff" cellpadding = "1" cellspacing = "1" width = "95%" align = "center">
<Tr height = "40"> <td bgcolor = "white"> User name: </td> <td bgcolor = "white"> <% = Html. textBoxFor (m => m. userName) %> </td> </tr>
<Tr height = "40"> <td bgcolor = "white"> Avatar: </td> <td bgcolor = "white"> <input type = "file" name = "up"/> </td> </tr>
<Tr height = "40"> <td bgcolor = "white"> Number of votes: </td> <td bgcolor = "white"> <% = Html. textBoxFor (m => m. voteCount) %> </td> </tr>
<Tr height = "40"> <td bgcolor = "white"> operation: </td> <td bgcolor = "white"> <input type = "submit" value = "Add User"/> </td> </tr>
</Table>
<% }%>
</Asp: Content>
In the above Code, MvcApplication16 is the project name and namespace. You can change it to your project name or namespace.
Source code: (no database, please build your own)
Download source code: http://www.bkjia.com/uploadfile/2011/1129/20111129031839448.zip
From Zhang Jian's column