gpt4 book ai didi

javascript - 在 _Layout 上添加对 Controller 的调用?

转载 作者:行者123 更新时间:2023-12-02 17:32:59 25 4
gpt4 key购买 nike

我有一个由我的 MVC 站点使用的布局页面。它没有模型或任何相关的 Controller 方法。

但现在我想在顶部添加一个“搜索”框。我无法添加普通的“表单”,因为它被覆盖,或者覆盖内容页面上的任何其他表单。所以我想我可能必须使用 Javascript 来做这件事...调用一个 javascript 函数,然后将查询发送到 Controller ,然后将用户移动到结果屏幕。

这是正确的方法吗?或者我可以以某种方式从我的布局页面使用正常的 Controller 方法调用吗?

最佳答案

假设您有以下模型:

public class SearchModel
{
public string SearchTerm { get; set; }
}

public class LoginModel
{
public string UserName { get; set; }

public string Password { get; set; }
}

并且您还有这些 Controller :

public class HomeController : Controller
{
public ActionResult Search(SearchModel model)
{
return View();
}
}

public class AccountController : Controller
{
public ActionResult Login(LoginModel model)
{
return View();
}
}

你可以使用这样的东西作为布局 View :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewBag.Title</title>
</head>
<body>

@using(Html.BeginForm("search", "home", FormMethod.Post, new {}))
{
<input type="search" name="SearchTerm" placeholder="Find..." />
<button type="submit">GO</button>
}

@if(!Request.IsAuthenticated)
{
using(Html.BeginForm("login", "account", FormMethod.Post, new {}))
{
<input type="text" name="UserName" placeholder="..." />
<input type="password" name="Password" placeholder="..." />
<button type="submit">GO</button>
}
}

@RenderBody()

</body>
</html>

模型绑定(bind)器足够智能,可以将请求参数与正确的模型属性相匹配,即使您不使用 @Html 帮助程序来创建 HTML 控件也是如此。您将遇到的问题是如何处理无效的登录尝试。看起来典型的工作流程是有一个正常的登录操作 - 如果用户在导航栏登录表单中输入无效凭据,您可以将用户转储到正确的登录页面。

另一个选项是将搜索和登录表单嵌入为子操作:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewBag.Title</title>
</head>
<body>

@Html.Action("search", "home")

@Html.Action("login", "account")

@RenderBody()

</body>
</html>

如果您走这条路,您将修改 Controller 以返回部分 View 。这是 AccountController 的一种实现:

public class AccountController : Controller
{
[HttpGet, AllowAnonymous]
public ActionResult Login(string returnUrl = "")
{
return View();
}

[HttpPost, AllowAnonymous]
public ActionResult Login(LoginModel model)
{
if(ModelState.IsValid)
{
/* valid user credentials */
return RedirectToAction("index", "home");
}

return View(model);
}

[AllowAnonymous, ChildActionOnly]
public ActionResult NavbarLogin()
{
return PartialView();
}
}

注意最后一个操作方法;将其标记为 ChildActionOnly 可防止浏览器直接请求该操作。该方法的 View 可能如下所示:

@model Your.Fully.Qualified.Namespace.Models.LoginModel

@using(Html.BeginForm("login", "account", FormMethod.Post, new { }))
{
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)

@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)

<button type="submit">Login</button>
}

强类型助手的优点是 MVC 将创建 Unobtrusive Validation 库利用的验证 data- 属性;在布局中包含 jqueryval 包,任何具有强类型帮助器的 View 都会自动获得客户端验证。

在此示例中,“导航栏登录”发布到正常的登录操作,因此无效的凭据将导致用户显示登录页面,而不是他们最初查看的页面。

关于javascript - 在 _Layout 上添加对 Controller 的调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22888230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com