mvc 怎么把后台拼接好的div写到前台_MVC 从后台页面 取前台页面传递过来的值的几种取法...
<1>前臺頁面 Index視圖
注意:用戶名表單的name值為txtName
密碼表單的name值為txtPassword
Test用戶名
密?碼
<2>后臺頁面,Home控制器 (為了測試,分別將視圖頁中的from表單的action設為 action="/Home/Test" ,action="/Home/Test2" action="/Home/Test3" action="/Home/Test4"?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
///
/// MVC第一種取值方式
///
///
public ActionResult Test()
{
string userName = Request["txtName"]; //此時Request["txtName"]=ABC
string password = Request["password"]; //此時Request["password"]=123
return Content("OK" + userName + password);
}
///
/// 第二種取值方式
///
///
///
public ActionResult Test2(FormCollection f) //FormCollection是MVC中表單里的一個集合,它也可以來接收前臺提交過來的表單,前臺提交過來的表單全都封裝到這個對象中來了
{
string userName = f["txtName"]; //此時f["txtName"]=ABC
string password = f["txtPassword"]; //此時f["txtPassword"]=123
return Content("OK" + userName + password);
}
///
/// 第三種取值方式
///
///
///
///
public ActionResult Test3(string txtName, string txtPassword) //注意這個參數的名稱必須要與前臺頁面控件的 name值是一致的
{
return Content("OK" + txtName + txtPassword);
//此時textName=ABC
//此時txtPassword=123
}
///
/// 第四中方式
///
///
///
///
///
public ActionResult Test4(string txtName, string txtPassword, ParaClass p) //如果ParaClass類里面的屬性與前臺頁面控件的name值一致,那么它的對象屬性也會自動被賦值
{
return Content("OK" + txtName + txtPassword + p.txtName + p.txtPassword);
//此時textName=ABC
//此時txtPassword=123
//此時p.txtName=ABC
//此時p.txtPassword=123
}
public class ParaClass
{
public string txtName { get; set; } //此時textName=ABC
public string txtPassword { get; set; } //此時txtPassword=123
}
}
}
版權聲明:本文為博主原創文章,未經博主允許不得轉載。
總結
以上是生活随笔為你收集整理的mvc 怎么把后台拼接好的div写到前台_MVC 从后台页面 取前台页面传递过来的值的几种取法...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西游日记0728
- 下一篇: php多线程 static变量,priv