ASP.NET MVC Action以强类型的方式接收表单数据
来源:互联网 发布:免费医疗软件 编辑:程序博客网 时间:2024/05/01 01:10
Action方法,以强类型的方式接收表单数据
第一种方法:通过FormCollection类型的Action方法参数来接受全部表单数据,而后使用UpdateModel()把表单数据写入强类型实例。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository.GetDinners(id);
try
{
UpdateModel(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
return View(dinners);
}
}
第二种方法:直接使用一个强类型作为Action方法的参数来把表单数据写入强类型参数变量中。
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Dinners dinners)
{
if (ModelState.IsValid)
{
try
{
dinners.HostedBy = "SomeUser";
dinnerRepository.Add(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
}
}
return View(dinners);
}
默认情况下,第一种情况UpdateModel()方法试图把所有表单项都匹配到强类型的属性。第二种情况也是如此,会自动试图把所有表单项全部都应赋值给强类型参数变量。
但是有时,我们可能只想或者表单项中只有一部分数据能都应赋值给强类型属性集。
在表单数据被映射使用的地方作出限定
使用UpdateMode时:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinners dinners = dinnerRepository.GetDinners(id);
//指定模型对象可更新的属性列表
String[] allowPropery = new[] { "Title", "Description", "ContactPhone" };
try
{
//使用UpdateModel重载方法
UpdateModel(dinners,allowPropery);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
return View(dinners);
}
}
Action参数为强类型时:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create([Bind(Include="Title,Description,ContactPhone")]Dinners dinners)
{
if (ModelState.IsValid)
{
try
{
//没有通过表单赋值的属性,可以手动赋值
dinners.HostedBy = "SomeUser";
dinnerRepository.Add(dinners);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinners.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinners.GetRuleViolations());
}
}
return View(dinners);
}
- 直接在类的定义时,作出限定s
通过添加[Bind]属性到类上,或者在应用程序中的Global.asax文件中(如果类型不是我们自定义的情况下,非常有用),使用Bind属性的Include和Exclude属性来指定类或接口中的那些属性是可以绑定的。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Linq;
namespace NerdDinner.Models
{
[System.Web.Mvc.Bind(Include="Title,Description,ContactPhone")]
public partial class Dinners
{
}
}
- ASP.NET MVC Action以强类型的方式接收表单数据
- ASP.NET MVC 在控制器中接收视图表单POST过来的数据方法
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- ASP.NET MVC中在Action获取提交的表单数据方法总结 .
- ASP.NET MVC中在Action获取提交的表单数据方法总结
- ASP.NET MVC中在Action获取提交的表单数据方法
- ASP.NET MVC 基于强类型视图下 对模型以及模型中List 字段的同时编辑 表单提交
- 关于ASP.Net MVC 强类型视图
- Asp.net mvc 强类型View
- Asp.Net MVC控制器中Action的返回值类型
- ASP.NET mvc下action的跳转方式
- ASP.NET mvc下action的跳转方式
- ASP.NET mvc下action的跳转方式
- ASP.NET MVC 表单的几种提交方式
- ASP.NET MVC Jquery Validate 表单验证的多种方式
- ASP.NET MVC 表单验证方式总结
- 强类型ASP.NET数据绑定
- [ASP.NET MVC 3.0]JsonValueProviderFactory讓Action接收Json更輕鬆
- 开发者必备的SQL性能优化基本功
- httpservletrequest cannot be resolved to a type
- SQL Server 2008 定时作业的制定(SQL2005参考此方法)
- Chrome源代码分析之进程和线程模型(四)
- oracle xml处理
- ASP.NET MVC Action以强类型的方式接收表单数据
- zoj2016Play on words(判断有向图是否存在欧拉回路通路)
- 又回到这里
- C#中计算时间段内工作日期
- 如何安装虚拟打印机
- 用jquery取radio check box值的一个注意事项
- suse 安装amarok并支持mp3等大多数格式
- 串口调试程序的实现
- 国际C 语言乱码大赛(IOCCC)1988年获奖作品