【发布时间】:2019-02-19 17:51:57
【问题描述】:
我是 MVC 的初学者,我正在使用 MVC 5
_Layout.cshtml 代码
_Layout.cshtml有导航栏,如果用户已经登录,它包含注销,更多按钮,另外,它会渲染身体部分(Index.cshtml)
//Navbar
@if (ViewBag.name != null)
{
<input type="submit" class="dropdown-item" formaction="Logout" value="Logout" id="btnLogout" />
}
else{
<a class="nav-link" href="#">Login & Signup</a>
}
<input type="submit" class="dropdown-item" formaction="More" value="More" id="btnMore" />
//body
<div class="container body-content">
@RenderBody() //Index.cshtml
</div>
Index.cshtml
Index.cshtml它有一些链接,还有一些按钮和锚链接,它会调用部分视图(_Login)
@using TestProject.Models
@model ViewSignInAndSignUp
//some html code
<input type="submit" class="dropdown-item" formaction="Action1" value="Action1" id="btnAction1" />
<input type="submit" class="dropdown-item" formaction="Action2" value="Action1" id="btnAction2" />
@Html.Partial("_Login") //partialview
_Login.cshtml
_Login.cshtml这个局部视图,如果用户访问任何链接,这个局部视图会弹出请求用户登录。这个局部视图按钮正在工作
@model TestProject.Models.SignIn
@using (Html.BeginForm("ControllerSignIn", "Home"))
{
@Html.TextBoxFor(si => si.userName, new { @class = "form-control", @id = "txtLogin" })
@Html.TextBoxFor(si => si.password, new { @class = "form-control", @id = "txtPassword", @type = "password" })
<input type="submit" class="btn btn-sm btn-primary btn-rounded" value="Login" id="btnLoginSubmit" />
}
HomeController
[HttpPost]
public ActionResult Logout(string submitButton)
{
// some coding
return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult More(string str1)
{
// some coding
return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult Action1(string str1)
{
// some coding
return RedirectToAction("Index", "Home");
}
[HttpPost]
public ActionResult Action2(string str1)
{
// some coding
return RedirectToAction("Index", "Home");
}
现在,我如何管理来自_Layout.cshtml, Index.cshtml, _Login.cshtml(Partial view) 的所有按钮
注意
我的登录按钮可以使用,但是注销、更多、action1、action2 按钮无法使用
【问题讨论】:
-
一个 submit 按钮提交 a
form。因此,不清楚您的问题是否真的只是将元素设置为“看起来”像按钮,或者每个按钮的多个表单。 -
@Edsf 我只需要为每个按钮传递一些参数,比如登录按钮(登录按钮将传递参数用户名和密码)。以同样的方式我必须执行其他按钮
-
如果你想调用单独的控制器动作,你必须将每个输入
type='submit'包装成自己的形式。 -
如上述评论所述,或者,您可以在 AJAX 帖子中创建所需的任何有效负载。
-
@Liamneesan 如果你不想使用 AJAX,你所有的部分都必须有一个表单来对应它的提交按钮。正如之前所说,提交按钮提交表单,除非您在 javascript 上编写自己的帖子,否则无法绕过它
标签: c# asp.net-mvc model-view-controller asp.net-mvc-5