我是mvc的新手。我喜欢从布局页面提交一个带有按钮的表单。需要以某种方式将模型传递给视图的控制器。任何提示或想法?
@if (!WebSecurity.IsAuthenticated) {
<form class="navbar-form navbar-right">
<div class="form-group">
<input type="text" class="form-control" placeholder="Benutzername">
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Passwort">
</div>
<input type="submit" class="btn btn-success" value="Login"/>
<input type="submit" class="btn btn-danger" value="Registrieren" />
</form>
}
else {
<form class="navbar-form navbar-right">
<input type="submit" class="btn btn-danger" value="Logout">
</form>
}
public class LoginModel
{
[Required]
[Display(Name = "Benutzername")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Kennwort")]
public string Password { get; set; }
[Display(Name = "Speichern?")]
public bool RememberMe { get; set; }
}
public class RegisterModel
{
[Required]
[Display(Name = "Benutzername")]
public string UserName { get; set; }
[Required]
[StringLength(100, ErrorMessage = ""e{0}"e muss mindestens {2} Zeichen lang sein.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Kennwort")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Kennwort bestätigen")]
[Compare("Password", ErrorMessage = "Das Kennwort entspricht nicht dem Bestätigungskennwort.")]
public string ConfirmPassword { get; set; }
}
答案 0 :(得分:1)
每个name
标记的属性input
绑定到Model
类的属性中,因此,只需在要绑定的属性名称中添加name
属性,样品:
<form class="navbar-form navbar-right">
<div class="form-group">
<input type="text" class="form-control" name="UserName" placeholder="Benutzername">
</div>
<div class="form-group">
<input type="text" class="form-control" name="Password" placeholder="Passwort">
</div>
<input type="submit" class="btn btn-success" value="Login"/>
<input type="submit" class="btn btn-danger" value="Registrieren" />
</form>
我建议您将此代码更改为PartialView
,而不是保留在Layout
页面上。这很有用,因为您可以使用模型键入PartialView
并使用Html
帮助器生成输入标记,例如Html.TextBoxFor(model => model.UserName, ...)
。有很多Html Helpers可以帮助您Html.BeginForm() method我在这里说过,这是一个很好的实践,但是您的代码运行正常,因为这些良好的实践产生了像您这样的代码。
答案 1 :(得分:1)
布局模型必须与页面模型相同。因此,这不是特别实用,除非您想要将基本模型用于一切不好的做法。
更好的方法是使用Html.Action()
并为登录表单使用单独的控制器操作,然后它可以拥有自己的模型和自己的执行上下文。
答案 2 :(得分:1)
几个PartialViews怎么样?这是一个粗略/一般的例子:
public class Login {
public string UserName { get; set; }
public string Password { get; set; }
}
public class Register : Login {
public string ConfirmPassword { get; set; }
}
登录部分:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Login</legend>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="submit" value="Login" />
</p>
</fieldset>
}
部分注册:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Register</legend>
<div class="editor-label">
@Html.LabelFor(model => model.ConfirmPassword)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ConfirmPassword)
@Html.ValidationMessageFor(model => model.ConfirmPassword)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
}
引用布局(或任何其他视图)中的部分内容:
@Html.Partial("LoginPartial")
@Html.Partial("RegisterPartial")