我可以设置从提交未执行控制器的表单加载的用户的身份验证状态。加载主页时,一切都正常工作(SuccessfullyLogged
),但是当我提交表单时,View被加载但只有View和控制器没有被执行而我得到SuccessfullyLogged = false
:
编辑:
页面结构为:
<Layout>
<Login Header Partial View>
@{ Html.RenderAction(MVC.Account.LoginHeader()); }
</Login Header Partial View>
<Body>
@RenderBody() <-- GetAllByCategory.cshtml rendered inside of it
</Body>
</Layout>
流程的逻辑:
CategoryGetAllBySearch.cshtml --> Controller GetAllByIds -> View GetAllByCategory inside of the Layout
编辑:
登录标题的布局上下文:
<!-- Login button -->
<ul class="menudrt" id="headerLogin">
@{ Html.RenderAction(MVC.Account.LoginHeader()); }
</ul>
<!---->
登录标题视图:
@model Heelp.ViewModels.LoginViewModel
<input type="hidden" id="IsUserAuthenticated" value="@Model.SuccessfullyLogged.ToString()" />
登录标题控制器:
[AllowAnonymous]
public virtual PartialViewResult LoginHeader()
{
var model = new LoginViewModel();
if (WebSecurity.IsAuthenticated)
{
model.SuccessfullyLogged = true;
}
else
model.SuccessfullyLogged = false;
return PartialView(model);
}
使用提交的表单查看CategoryGetAllBySearch.cshtml:
@model Heelp.ViewModels.CategoryGetAllBySearchListViewModel
@if (@Model.TotalSearchCount == 0)
{
<p>lamentamos mas não foram encontrados resultados para a sua pesquisa<br />
sugestões: <br />
» veja a lista de carros, motos ou comerciais a partir do menu<br />
» tente utilizar outras ou menos palavras</p>
}
else
{
<p>encontrámos <strong style="font-style:italic;">@Model.TotalSearchCount</strong> resultado(s) na(s) categoria(s):</p><!-- total results main text -->
foreach (var item in Model.CategoryGetAllBySearch)
{
@using (Html.BeginForm("GetAllByIds", "Ads", FormMethod.Post, new { id = "homeCategoryForm_" + item.Id }))
{
@Html.AntiForgeryToken()
@Html.Hidden("ids", string.Join("-", item.Ids))
@Html.Hidden("categoryId", item.Id)
@Html.Hidden("search", (string)ViewBag.search)
@Html.Hidden("location", (string)ViewBag.location)
}
<div id="category_@item.Id" class="innerelement">
<p id="category-p_@item.Id"><strong style="font-style:italic;">@Html.DisplayFor(modelItem => item.SearchCount)</strong> @Html.DisplayFor(modelItem => item.Name)<span>ver</span></p>
<img id="category-img_@item.Id" src="~/Content/Images/ui-symb-arrow-right-green-12x12.png" width="12" height="12" />
</div>
}
}
此形式的控制器GetAllByIds:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public virtual ActionResult GetAllByIds(string ids, int categoryId, string search, string location)
{
AdGetAllByCategoryListViewModel model = new AdGetAllByCategoryListViewModel();
model.Ads = Mapper.Map<IList<AdGetAllByCategoryDto>, IList<AdGetAllByCategoryViewModel>>(_adService.GetAllByIds(ids));
model.Category = Mapper.Map<CategoryDto, CategoryViewModel>(_categoryService.GetById(categoryId));
return View(MVC.Ad.Views.GetAllByCategory, model);
}
那么为什么表单提交时没有调用控制器/视图而只调用视图?
由于
答案 0 :(得分:0)
在@Racil Hilan的帮助下进行了一些测试后,我在这里找到了问题。
问题在于我有两个名为LoginHeader的动作,一个具有[HttpPost]属性,另一个没有它。
[AllowAnonymous]
public virtual PartialViewResult LoginHeader()
{
var model = new LoginViewModel();
if (WebSecurity.IsAuthenticated)
{
model.SuccessfullyLogged = true;
MemberDto memberDto = _memberService.GetMemberById(WebSecurity.CurrentUserId);
model.MemberType_Id = memberDto.MemberType_Id;
}
else
model.SuccessfullyLogged = false;
return PartialView(model);
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public virtual PartialViewResult LoginHeader(LoginViewModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.Email, model.Password, persistCookie: model.RememberMe))
{
// The user is valid so it will be redirected based on the returnUrl. If null it will return to the Home-Page
model.SuccessfullyLogged = true;
model.Result = @HeelpResources.AccountLoginViewAutenticationValidMsg;
}
else
{
// Invalid credentials, so the user will have to try again
model.SuccessfullyLogged = false;
model.Result = @HeelpResources.AccountLoginViewAutenticationInvalidMsg;
}
return PartialView(model);
}
在这种情况下,当Post提交表单时,系统会进入标有[HttpPost]的表单,并且没有进行正确的验证。
所以建议是,当你使用两个同名的动作时要非常小心,因为系统可能会调用你不期望的那个。