gpt4 book ai didi

javascript - 返回部分 View 和消息

转载 作者:行者123 更新时间:2023-11-30 07:22:23 25 4
gpt4 key购买 nike

我有一个局部 View ,可以在其中更改连接字符串。提交时调用 Edit 操作。如果我想让用户再试一次,我想从这里返回并重新打开局部 View 。如果一切顺利(或崩溃),我想调用我的 JavaScript 函数 Logout,将用户注销并重定向到某个起始页。

这两种解决方案都有效,只是不能同时使用。我显然缺少一些最佳实践,我该怎么办?

局部 View :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.");
}

最佳答案

使用扩展方法RenderToString并基于 cacois answer , 你可以这样创建你的 Action :

public ActionResult Edit(SettingViewModel model)
{
// "Ifs" to return only partials
if (ModelState.IsValid)
{
return PartialView("EditSetting", model);
}

...

// Returning a Json with status (success, error, etc), message, and the content of
// your ajax, in your case will be a PartialView in string
return Json(new {
Status = 1,
Message = "error message",
AjaxReturn = PartialView("EditSetting", model).RenderToString()});
}

附言。我建议您创建一个模型来定义 Ajax 返回,包括 StatusMessageAjaxReturn。这样,您的 ajax 请求将始终返回相同的对象类型。对于 Status 属性,您可以创建一个枚举。

您的 ajax 请求将是这样的:

$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (data) {
if(data.Message == undefined) {
// Use data like a partial
} else {
// Use data.Message for the message and data.AjaxReturn for the partial
}
},
error: function (message) {
logOut($self, message);
}
});

关于javascript - 返回部分 View 和消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34066416/

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