如何发布更多行

时间:2009-12-15 10:20:19

标签: c# asp.net asp.net-mvc

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit( string id_stud)
    {

            ViewData["stud"] = id_stud;

        return View("temp");   
     }

temp.aspx:

<h2><%= Html.Encode(ViewData["stud"]) %></h2>

此页面仅显示一名学生。如何展示其他学生?

2 个答案:

答案 0 :(得分:0)

使用数组:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit( string id_stud)
{
    ViewData["student_ids"] = new int[] { 1, 2, 3, 4 };
    return View("temp");   
}

<% foreach (int stud_id in (int[])ViewData["student_ids"]) { %>
    <h2><%= Html.Encode(stud_id.ToString()) %></h2>
<% } %>

答案 1 :(得分:0)

临时视图的内容应如下所示:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.Student>>" %>

<table>
<% foreach (var item in Model) { %>
    <tr>
        <td>
            <%= Html.Encode(item.Id) %>
        </td>
        <td>
            <%= Html.Encode(item.FirstName) %>
        </td>
        <td>
            <%= Html.Encode(item.LastName) %>
        </td>
        <td>
            <%= Html.Encode(item.Age) %>
        </td>
    </tr>
<% } %>
</table>

编辑视图内容:

<% using (Html.BeginForm())
  { %>
   <fieldset>
        <% foreach (Student s in Model)
           { %>
           <%= Html.Hidden("Id", s.Id)%>
           <p>
               <span>First Name</span><br />
               <%= Html.TextBox("FirstName", s.FirstName)%>
           </p>
           <p>
               <span>Last Name</span><br />
               <%= Html.TextBox("LastName", s.LastName)%>
           </p>
           <p>
               <span>Age</span><br />
               <%= Html.TextBox("Age", s.Age)%>
           </p>
           <hr />
        <% } %>
        <input type="submit" value="Submit" />
    </fieldset>
<% } %>

学生班级结构:

namespace MvcApplication1.Models
{

    public class Student
    {

        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

    }

}

最后,控制器:

using System.Collections.Generic;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{

    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            List<Student> students = new List<Student>();

            // Fill with dummy data for test.
            students.Add(new Student
            {
                Id = 1,
                FirstName = "X",
                LastName = "X",
                Age = 20
            });

            students.Add(new Student
            {
                Id = 2,
                FirstName = "Y",
                LastName = "Y",
                Age = 30
            });

            return View(students);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Index(int[] id, string[] firstName, string[] lastName, int[] age)
        {
            List<Student> students = new List<Student>();

            for (int i = 0; i < id.Length; i++)
            {
                students.Add(new Student
                {
                    Id = id[i],
                    FirstName = firstName[i],
                    LastName = lastName[i],
                    Age  = age[i]
                });
            }

            return View("Shows", students);
        }

    }

}