如果用户没有正确输入页面上的所有内容,我有一个使用MVC 5验证的特定问题来显示错误消息。表单的HTML标记如下:
@using (Html.BeginForm("RegisterDo", "User", FormMethod.Post))
{
<div>
@Html.ValidationMessageFor(model => model.FirstName)
@Html.TextBoxFor(m => m.FirstName, new { placeholder = "First name", @class = "form-control", @type = "text" })
</div>
<div>
@Html.ValidationMessageFor(model => model.LastName)
@Html.TextBoxFor(m => m.LastName, new { placeholder = "Last name", @class = "form-control", @type = "text" })
</div>
<div>
@Html.ValidationMessageFor(model => model.Email)
@Html.TextBoxFor(m => m.Email, new { placeholder = "Email", @class = "form-control", @type = "email" })
</div>
<div>
@Html.ValidationMessageFor(model => model.Password)
@Html.TextBoxFor(m => m.Password, new { placeholder = "Password", @class = "form-control", @type = "password" })
</div>
<div>
@Html.ValidationMessageFor(model => model.PasswordConfirm)
@Html.TextBoxFor(m => m.PasswordConfirm, new { placeholder = "Confirm password", @class = "form-control", @type = "password" })
</div>
<div>
@Html.ValidationMessageFor(model => model.SelectedCountryId)
@Html.DropDownListFor(model => model.SelectedCountryId, Model.Countries, "-- Please select a country --", new { @class = "select2_single form-control select2-hidden-accessible", @tabindex = "-1" })
</div>
<div>
<input class="btn btn-default submit" type="submit" value="Register" />
</div>
}
请注意,我有一个下拉列表,我需要在渲染视图时首先填充(即我必须将国家/地区列表传递到视图以便正确呈现)。我在这里遇到的问题是,当我点击“注册”按钮时,我的动作中的代码不会被触发。相反,我只需在浏览器中获取以下URL:
http://localhost:60617/user/register?FirstName=&LastName=&Email=&Password=&PasswordConfirm=&SelectedCountryId=
根本不显示错误消息......根本不会触发代码...
这是我的两个行动:
[HttpPost]
[ActionName("Register")]
public ActionResult DoRegister(UserRegistrationViewModel model)
{
if (ModelState.IsValid)
{
var user = new Users();
user.FirstName = model.FirstName;
user.LastName = model.LastName;
user.Email = model.Email;
user.PasswordSalt = Helpers.PasswordHelper.CreateSalt(40);
user.PasswordHash = Helpers.PasswordHelper.CreatePasswordHash(model.Password, user.PasswordSalt);
user.CountryId = Convert.ToInt32(model.SelectedCountryId);
user.Active = true;
Connection.ctx.Users.Add(user);
Connection.ctx.SaveChanges();
var role = new UserRoles();
role.RoleId = 2;
role.UserId = user.UserId;
role.Active = true;
user.UserRoles.Add(role);
Connection.ctx.SaveChanges();
return RedirectToAction("Success");
}
else
{
return View("Register", model);
}
}
public ActionResult Register()
{
var model = new UserRegistrationViewModel();
var countries = Connection.ctx.Countries.OrderBy(x => x.CountryName).ToList();
model.Countries = new SelectList(countries, "CountryId", "CountryName");
return View(model);
}
第一个动作负责渲染视图,第二个动作负责用户注册时的逻辑......
我在这里做错了什么。我错过了一些非常明显的东西,但我只是看不到...... :(
这里编辑的是ViewModel:
public class UserRegistrationViewModel
{
[Required(ErrorMessage = "First name is required!")]
public string FirstName { get; set; }
[Required(ErrorMessage = "Last name is required!")]
public string LastName { get; set; }
[Required(ErrorMessage = "Email name is required!")]
public string Email { get; set; }
[Required(ErrorMessage = "Password name is required!")]
public string Password { get; set; }
[Required(ErrorMessage = "Password confirmation name is required!")]
public string PasswordConfirm { get; set; }
[Required(ErrorMessage = "Country needs to be selected!")]
public int SelectedCountryId { get; set; }
public SelectList Countries { get; set; }
}
答案 0 :(得分:1)
else
{
var countries = Connection.ctx.Countries.OrderBy(x => x.CountryName).ToList();
ViewBag.Countries = new SelectList(countries, "CountryId", "CountryName");
return View("Register", model);
}
再次将模型传递给您的视图
答案 1 :(得分:1)
一切都很好,除了应该
的其他部分return View("Register", model);
需要传递模型才能查看这些错误消息。 并采取Hit动作方法 将视图中的操作名称更改为&#34; RegisterDo&#34;
或
添加
[ActionName("Register")]
[HttpPost]
public ActionResult RegisterDo(UserRegistrationViewModel model)
答案 2 :(得分:1)
在帖子方法中添加import time
from bottle import route, run, template, view, request
@route('/')
@view('test.html')
def index():
context = {'request': request}
return (context)
def worker_loop():
work_on_the_db_each_10_seconds()
time.sleep(10)
run(host='localhost', port=9000, debug=True)
属性:
[ActionName("Register")]