私はしばらくMVC 2を使用しており、ReturnToActionとValidationSummaryを実行しましたが、これは「送信」ボタンがjavascript/JQueryによるコントロールであるという点で少し異なります-アクションをデバッグし、正しいController Action ですが、RedirectToAction を通過すると、何も起こりません....
私の2番目の問題は、ValidationSummaryが表示されないことです-テストを実行し、ModelStateが無効なときにViewを返すと、何も表示されません
ボタン/フォーム/送信/JQuery に問題がありますか?
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript">
$(function () {
/*stuff here to setup some JQuery Sortable lists*/
$("#UpdateButton").click(function () {
//create Arrays from JQuery Sortable List and go to Action for "submit" //processing
$.ajax({
url: '/Admin/SortedLists/',
data: { items: editedRoles, items2: $("#deleteList").sortable('toArray') },
type: 'POST',
traditional: true
});
});
//go to Action and just "Reload" the page
$("#UndoButton").click(function () {
//reload the page
var url = '<%= Url.Action("EditRoles") %>';
window.location.href = url;
});
$("#roleList, #deleteList").disableSelection();
$("#deleteList").hide();
});
function addNewRole() {
var text = $("#New_Role").val();
$("#roleList").append('<li id="-1~' + text + '" class="ui-state-default">' +
'<span class="ui-icon ui-icon-closethick"></span>' +
// '<span class="ui-icon ui-icon-arrowthick-2-n-s"></span>' +
'<input id="-1" type="text" value="' + text + '" />' +
'</li>');
$("#roleList").sortable('refresh');
}
</script>
<%= Html.ActionLink("Back", "Index") %>
<% using (Html.BeginForm()) { %>
<br />
<%= Html.Encode(ViewData["Message"]) %>
<%=Html.ValidationSummary(true, "Edit was unsuccessful. Please correct the errors and try again.")%>
<div class="demo">
<%=Html.TextBox("New Role", "New Role")%>
<a href="javascript:addNewRole()"> Add</a>
<br />
<br />
<ul id="roleList" class='droptrue'>
//create an unordered list with textboxes and a close icon
<%
foreach (var item in Model.Roles)
{%>
<li class="ui-state-default" id="<%=Html.AttributeEncode(item.Id)%>~<%=Html.AttributeEncode(item.Name)%>"><span class="ui-icon ui-icon-closethick"></span><%=Html.TextBox(item.Id.ToString(), item.Name, new {@id = item.Id})%></li>
<% } %>
</ul>
<ul id="deleteList" class='droptrue'>
</ul>
<br />
</div>
<input id="UpdateButton" type="submit" name="submitButton" value="Update" /><%= Html.ValidationMessage("UpdateButton", "*") %>
<input id="UndoButton" type="submit" name="submitButton" value="Undo" />
<% } %>
コントローラーは次のようになります。
public AdminController()
{
var wrapper = new ModelStateWrapper(ModelState);
_rolesService = new RolesService(new RolesRepository(), new RolesValidator(wrapper, new DateValidator(wrapper)));
}
public ActionResult Index()
{
return View();
}
public ActionResult EditRoles()
{
var roles = _rolesService.FetchAllRoles();
return View(new AdminEditRolesViewModel(roles));
}
[HttpPost]
public ActionResult SortedLists(List<string> items, List<string> items2)
{
var roles = _rolesService.BuildRolesFromList(items);
var deletedRoles = _rolesService.BuildRolesFromList(items2);
//The Services have contain the ModelState, this is where errors happen
//ValidationSummary doesnt show anything
if (_rolesService.EditRoles(roles) == false)
{
roles = _rolesService.FetchAllRoles();
return View("EditRoles", new AdminEditRolesViewModel(roles));
}
if (_rolesService.DeleteRoles(deletedRoles) == false)
{
roles = _rolesService.FetchAllRoles();
return View("EditRoles", new AdminEditRolesViewModel(roles));
}
_rolesService.Save();
//This RedirecToAction is passed, but doesnt actually go to my Index()
return RedirectToAction("Index");
}
私のサービスは検証などを処理し、ModelState と ModelStateDictionary ラッパーを渡し、エラーを追加します - エラーを間違って追加していませんか?
public bool DeleteRoles(IEnumerable<Role> deletedRoles)
{
//some work, if fails add error
_validator.AddError("UpdateButton",
"Role: " + role.Name +
" can not be deleted because Employees still use this";
return _validator.IsValid();
}
助けてくれてありがとう - これは私を壁に追いやっています