Asp.net mvc 3 返回视图时如何保持相同的数据?
返回视图时如何保持相同的数据 我试图将表单返回到视图,但没有成功 有什么好的简单的方法可以做到这一点吗Asp.net mvc 3 返回视图时如何保持相同的数据?,asp.net-mvc-3,Asp.net Mvc 3,返回视图时如何保持相同的数据 我试图将表单返回到视图,但没有成功 有什么好的简单的方法可以做到这一点吗 [HttpPost] public ActionResult Register(FormCollection form) { string name = form["Name"].Trim(); if (string.IsNullOrEmpty(name)) { TempData["TempData"] = "Please provide your na
[HttpPost]
public ActionResult Register(FormCollection form)
{
string name = form["Name"].Trim();
if (string.IsNullOrEmpty(name))
{
TempData["TempData"] = "Please provide your name ";
return View(form);
}
string email = form["Email"].Trim();
var isEmail = Regex.IsMatch(email, @"(\w+)@(\w+)\.(\w+)");
if (!isEmail)
{
TempData["TempData"] = "Sorry, your email is not correct.";
return View(form);
}
//do some things
}
不知道为什么你会在帖子中使用
FormCollection
,但也许你有WebForms的背景。在MVC中,您应该使用ViewModels在视图之间传输数据
默认情况下,MVC 3应用程序中的Register
方法在Register
视图中使用ViewModel。你应该把它寄回去。事实上,默认应用程序已经为您创建了,如果您不知道它是Internet模板的一部分
标准模式是拥有一个ViewModel,它表示您将在视图中使用的数据。例如,在您的情况下:
public class RegisterViewModel {
[Required]
public string Name { get; set; }
[Required]
[DataType(DataType.EmailAddress)]
[Display(Name = "Email address")]
public string Email { get; set; }
}
您的控制器应该包含两个操作,一个Get
和一个Post
。Get
呈现视图,用户可以输入数据。提交视图后,将调用Post
操作。视图将ViewModel发送给操作,然后方法执行操作以验证和保存数据
如果数据存在验证错误,将ViewModel返回到视图并显示错误消息非常简单
以下是Get
操作:
public ActionResult Register() {
var model = new RegisterViewModel();
return View(model);
}
[HttpPost]
public ActionResult Register(RegisterViewModel model) {
if(ModelState.IsValid) { // this validates the data, if something was required, etc...
// save the data here
}
return View(model); // else, if the model had validation errors, this will re-render the same view with the original data
}
下面是Post
操作:
public ActionResult Register() {
var model = new RegisterViewModel();
return View(model);
}
[HttpPost]
public ActionResult Register(RegisterViewModel model) {
if(ModelState.IsValid) { // this validates the data, if something was required, etc...
// save the data here
}
return View(model); // else, if the model had validation errors, this will re-render the same view with the original data
}
你的观点是这样的
@model RegisterViewModel
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Name) <br />
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.Email) <br />
@Html.ValidationMessageFor(model => model.Email)
</div>
}
@model RegisterViewModel
@使用(Html.BeginForm()){
@Html.ValidationSummary(true)
@LabelFor(model=>model.Name)
@Html.TextBoxFor(model=>model.Name)
@Html.ValidationMessageFor(model=>model.Name)
@LabelFor(model=>model.Email)
@Html.TextBoxFor(model=>model.Email)
@Html.ValidationMessageFor(model=>model.Email)
}
使用其他策略在MVC应用程序中捕获和保存数据是完全可能的,它是一个非常可扩展的框架。但是有一种特定的模式使MVC成为现在的样子,而与这种模式对抗有时会很困难。对于初学者来说,最好先了解首选的模式和策略,一旦理解得很好,就可以采用一些自己的定制策略来满足自己的需要。到那时,你应该对系统有足够的了解,知道你需要改变什么,在哪里
快乐编码 只是好奇为什么要使用FormCollection而不是ViewModel。这将使您的生活更轻松(特别是有了这个问题:-)向我们展示呈现视图的操作,这样我们就可以根据您的代码给出正确的答案。这都取决于这个方法,我也有同样的感觉。寄存器的GET方法是如何实现的?你在那边用什么型号的?为什么您不能在POST中使用相同的模型?当您使用VS模板创建MVC3项目时,您会得到由VS模板(AccountController.cs)生成的AccountController,它在Register操作中使用RegisterModel模型类(AccountModel.cs),同样由模板生成,这可能是您想要实现的一个很好的示例<代码>[HttpPost]公共操作结果寄存器(RegisterModel){…返回视图(model);}或者我对该问题的理解不正确?感谢您的建议。:)