MVC-各种传值方式

【转自】:QLeelulu
示例一:
ViewData传值.
HomeController.cs Co

de:

public ActionResult Index()
{
     ViewData[ "Title"] = "Home Page";
     ViewData[ "Message"] = "Welcome to ASP.NET MVC!";
     return View();
}
Views/Home/Index.aspx Code:

<asp:Content ID= "indexContent" ContentPlaceHolderID= "MainContent" runat= "server">
    <p>
    <%= Html.Encode(ViewData[ "Message"]) %>
    </p>
</asp:Content>

结果:在页面上显示 Welcome to ASP.NET MVC!

示例二:
带参数传值.
URL Routing规则:

routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{param}",                           // URL with parameters
    new { controller = "Home", action = "Index", param = "" }  // Parameter defaults
);
HomeController.cs Code:

public ActionResult Index( string param, int? paraInt, string paraStr)
{
    ViewData[ "Title"] = "Home Page";
    ViewData[ "Message"] = "Welcome to ASP.NET MVC!";
    ViewData[ "Param"] = param;
    ViewData[ "ParaInt"] = paraInt;
    ViewData[ "ParaStr"] = paraStr;
    return View();
}
Views/Home/Index.aspx Code:

<asp:Content ID= "indexContent" ContentPlaceHolderID= "MainContent" runat= "server">
    <p>
    <%= Html.Encode(ViewData[ "Message"]) %>
    </p>
    <p>
    <%= Html.Encode(ViewData[ "Param"]) %>
    </p>
    <p>
    <%= Html.Encode(ViewData[ "ParaInt"] ?? ( object) "(null)")%>
    </p>
    <p>
    <%= Html.Encode(ViewData[ "ParaStr"] ?? ( object) "(null)")%>
    </p>
</asp:Content>
结果:
访问:/home/index/hello?paraint=520&parastr=world
显示: hello 520 world
访问:/home/index/hello
显示:hello (null) (null)

示例三:
强类型传值:
新建一个xml文件:
 
<?xml version="1.0" encoding="utf-8" ?>
<root>
  <item name="Sea">
    <animal>Fish</animal>
    <animal>Shrimp</animal>
    <animal>Crab</animal>
  </item>
  <item name="Sky">
    <animal>Bird</animal>
    <animal>Man</animal>
  </item>
</root>
新建一个类读取xml数据.
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Web.Hosting;

namespace ViewData.Models
{
    public class Space
    {
        public string Name { getset}
        public string[] Animal { getset}
        private static Space space;
        public IEnumerable<Space> GetSpace()
        {
            XDocument xml = XDocument.Load(HostingEnvironment.MapPath("~/App_Data/Space.xml"));
            IEnumerable<Space> results = from p in xml.Root.Elements("item")
                                         select new Space
                                         {
                                             Name = p.Attribute("name").Value,
                                             Animal = p.Elements("animal").Select(r => r.Value).ToArray()
                                         };
            return results;
        }
        public static Space Instance
        {
            get 
            {
                if (space == null)
                {
                    space = new Space();
                }
                return space;
            }
        }
    }
}
在HomeController内添加Action:
 
public ActionResult About()
{
    ViewData["Title"] = "About Page";

    return View(Space.Instance.GetSpace());
}
在About.aspx.cs后天修改如下.
 
public partial class About : ViewPage<IEnumerable<Space>>
About.aspx调用数据:
 
<%foreach (var p in ViewData.Model){%>
<ul>
  <li><%=p.Name %>
  <ul>
  <%foreach (string r in p.Animal){ %>
  <li><%=r%></li>
  <%} %>
  </ul>
  </li>
</ul>
<%} %>
结果:
MVC-各种传值方式
 
示例四:
表单的值回传给服务器.
新建一个UserM类:UserM.cs
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ViewData.Models
{
    public class UserM
    {
        public string Name { getset}
        public string Password { getset}
        private static UserM user;
        
        public static UserM Instance 
        {
            get 
            {
                if (user == null)
                {
                    user = new UserM();
                }
                return user;
            }
        }
    }
}
HomeControllers添加Action
 
[AcceptVerbs("GET")] //GET调用
public ActionResult Form()
{
    return View(UserM.Instance);
}

[ActionName("Form"), AcceptVerbs("POST")]//POST调用
public ActionResult SaveForm()
{
    UpdateModel(UserM.Instance, new[] { "Name""Password" });
    return View(UserM.Instance);
}

Form.aspx.cs后台添加强类型引用
 
public partial class Form : ViewPage<UserM>

Form.aspx
<p>
<%=Html.Encode(ViewData.Model.Name) %><br />
<%=Html.Encode(ViewData.Model.Password) %>
</p>
    <%Html.BeginForm();%>
    Name:<%=Html.TextBox("Name")%>
    Password:<%=Html.Password("Password") %>
    <input type="submit" value="Submit" />
    <%Html.EndForm(); %>
结果:在form表单的内容将显示到当前页面

你可能感兴趣的:(mvc)