Asp.net mvc 4 注册页面视图在主页面视图中不起作用
首先,我有一个这样的注册页面Asp.net mvc 4 注册页面视图在主页面视图中不起作用,asp.net-mvc-4,Asp.net Mvc 4,首先,我有一个这样的注册页面 @using (Html.BeginForm()) { @Html.AntiForgeryToken() <form role="form" action="" method="post" class="login-form"> <div class="form-group"> <div> @Html.TextBoxFor(model => model.Name,
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<form role="form" action="" method="post" class="login-form">
<div class="form-group">
<div>
@Html.TextBoxFor(model => model.Name, new { @class = "form-username form-control", placeholder = "Name" })
</div>
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.UserName, new { @class = "form-username form-control", placeholder = "User Name" })
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.Email, new { @class = "form-username form-control", placeholder = "Email" })
</div>
<div class="form-group">
@Html.PasswordFor(model => model.Password, new { @class = "form-username form-control", placeholder = "Password" })
</div>
<input type="submit" value="Register" class="btn" />
</form>
}
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.TextBoxFor(model=>model.Name,新的{@class=“formusername表单控件”,placeholder=“Name”})
@Html.TextBoxFor(model=>model.UserName,新的{@class=“form UserName form control”,placeholder=“User Name”})
@Html.TextBoxFor(model=>model.Email,新的{@class=“formusername表单控件”,placeholder=“Email”})
@Html.PasswordFor(model=>model.Password,新的{@class=“formusername表单控件”,placeholder=“Password”})
}
当我尝试注册该页面时,我注册并将帐户添加到数据库中。但我想在主页上使用该注册页面渲染,并像那样使用,但当我这样做时,它不起作用
<div class="form-bottom">
@RenderPage("~/Views/User/Register.cshtml")
</div>
@RenderPage(“~/Views/User/Register.cshtml”)
这是控制器代码
public ActionResult Register()
{
return View(new RegisterModel());
}
[HttpPost]
public async Task<ActionResult> Register(RegisterModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
var siteContext = new SiteContext();
var user = new User
{
Name = model.Name,
Email = model.Email,
Password=model.Password,
UserName=model.UserName
};
await siteContext.Users.InsertOneAsync(user);
return RedirectToAction("Profile", "MainPage");
}
public ActionResult寄存器()
{
返回视图(新的RegisterModel());
}
[HttpPost]
公共异步任务寄存器(RegisterModel模型)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
var siteContext=new siteContext();
var user=新用户
{
Name=model.Name,
Email=model.Email,
Password=model.Password,
用户名=model.UserName
};
等待siteContext.Users.InsertOneAsync(用户);
返回重定向到操作(“配置文件”、“主页”);
}
老实说,当您使用Html.BeginForm()
时,整个视图有点混乱,您不需要另一个表单。您的视图应该如下所示:
@using (Html.BeginForm("{Action}", "{Controller}", FormMethod.Post, new { @class = "login-form"}))
{
@Html.AntiForgeryToken()
<div class="form-group">
<div>
@Html.TextBoxFor(model => model.Name, new { @class = "form-username form-control", placeholder = "Name" })
</div>
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.UserName, new { @class = "form-username form-control", placeholder = "User Name" })
</div>
<div class="form-group">
@Html.TextBoxFor(model => model.Email, new { @class = "form-username form-control", placeholder = "Email" })
</div>
<div class="form-group">
@Html.PasswordFor(model => model.Password, new { @class = "form-username form-control", placeholder = "Password" })
</div>
<input type="submit" value="Register" class="btn" />
}
@使用(Html.BeginForm(“{Action}”,“{Controller}”,FormMethod.Post,new{@class=“login form”}))
{
@Html.AntiForgeryToken()
@Html.TextBoxFor(model=>model.Name,新的{@class=“formusername表单控件”,placeholder=“Name”})
@Html.TextBoxFor(model=>model.UserName,新的{@class=“form UserName form control”,placeholder=“User Name”})
@Html.TextBoxFor(model=>model.Email,新的{@class=“formusername表单控件”,placeholder=“Email”})
@Html.PasswordFor(model=>model.Password,新的{@class=“formusername表单控件”,placeholder=“Password”})
}
您需要将{Action}
替换为操作的名称Register。还将{Controller}
替换为控制器的名称
一些有用的。问题到底是什么?问题注册页本身可以工作,但当我在主页上呈现它时,它不工作。它不会将帐户添加到数据库。什么不工作?是否有错误消息?如果该项没有添加到数据库中,那么这就是我们需要看到的代码。没有。İ可以添加控制器代码。我知道我还是mvc的新手。谢谢它的工作。