【发布时间】:2016-03-08 02:12:09
【问题描述】:
我有一个局部视图,可以在其中更改连接字符串。当提交 Edit 动作时被调用。如果我希望用户再试一次,我想从这里返回并重新打开部分视图。如果一切顺利(或崩溃),我想调用我的 JavaScript 函数 Logout,这会将用户注销并重定向到某个起始页。
两种解决方案都有效,只是不能一起使用。我显然错过了一些最佳实践,我该怎么办?
局部视图:EditSetting
@model WebConsole.ViewModels.Setting.SettingViewModel
@using (Ajax.BeginForm("Edit", "Setting", new AjaxOptions { UpdateTargetId = "div" }, new { id = "editform" }))
{
<fieldset>
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new {@class = "text-danger"})
<div class="form-group">
@Html.LabelFor(model => model.User, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.User, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.User, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Password, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
<input type="password" name="Password" id="Password" value=""/>
@Html.ValidationMessageFor(model => model.Password, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.DataSource, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.DataSource, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.DataSource, "", new {@class = "text-danger"})
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.InitialCatalog, htmlAttributes: new {@class = "control-label col-md-2"})
<div class="col-md-10">
@Html.EditorFor(model => model.InitialCatalog, new {htmlAttributes = new {@class = "form-control"}})
@Html.ValidationMessageFor(model => model.InitialCatalog, "", new {@class = "text-danger"})
</div>
</div>
</div>
</fieldset>
}
JavaScript:提交
$('form').submit(function () {
var $self = $(this);
if ($(this).valid()) {
// Change Connection String
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (message) {
// Use Partial View
//$('#myModal .modal-body').html(message);
// Conn Str is now changed. Log out and redirect
logOut($self, message);
},
error: function (message) {
logOut($self, message);
}
});
}
return false;
});
操作:编辑
[HttpPost]
public ActionResult Edit(SettingViewModel model)
{
// Validate inputs
if (!ModelState.IsValid)
{
ModelState.AddModelError("", @"Not all inputs are valid.");
return PartialView("EditSetting", model);
}
var sql = new DAL.SQL(DAL.SQL.GenerateConnectionString(model.DataSource, model.InitialCatalog, model.User, SecurePassword(model.Password)));
// Validate Connection String
if (!sql.Open())
{
ModelState.AddModelError("", @"Error. Unable to open connection to Database.");
return PartialView("EditSetting", model);
}
// Validate a transaction
if (!sql.IsRunningTransact())
{
ModelState.AddModelError("", @"Error. Unable to connect to Database Server.");
return PartialView("EditSetting", model);
}
// Save Connection String
BuildAndEncryptConnString(model);
return Content("The Connection String is changed. Log in again to continue.");
}
【问题讨论】:
-
@RenanAraújo 我不需要将视图呈现为字符串。我需要知道客户端是否从操作返回了我的结果(消息),是我需要再次呈现的部分视图,还是我需要在注销用户并重定向之前向用户提示的消息他到起始页。 :)
-
@RenanAraújo 如果我错了,请告诉我,我只是说我现在看到的。可能我还没看懂。
-
嗨@radbyx 我发布了一个答案,评论太长了:)
-
真正隐藏的bug在这里,我在2½天后发现了它! "UpdateTargetId = "div"",因为它不适用于 "$('#myModal .modal-body').html(message);"部分视图被渲染,看起来一切都很好,但是第二个 ajax 调用不返回。它现在使用“UpdateTargetId”。 :)
标签: javascript asp.net-mvc asp.net-mvc-5 asp.net-mvc-partialview