gpt4 book ai didi

c# - 部分 View 中的按钮在搜索后停止工作

转载 作者:可可西里 更新时间:2023-11-01 13:47:53 29 4
gpt4 key购买 nike

我有一个布局页面和一个 PartialView。

我在布局页面上有一个搜索按钮,在局部 View 的表格中有一个更新按钮。

单击搜索按钮后,更新按钮将停止工作。在我搜索之前它工作正常。我猜它与表单和操作结果有关,但我无法让它工作。从未达到操作结果 - “什么都没有”发生。

Image of the problem

Controller - RegisterTruckUser

[HttpPost]
public ActionResult RegisterTruckUser(string registerName, string registerPassword, string employeeNumber, string userTeam)
{
var model = new RegisterModel
{
UserList = db.SearchTruckUsers(""),
UserTeamList = GetUserTeamList(),
TriggerOnLoad = true
};

//If the Save button is clicked the user is updated
if (Request.Form.AllKeys.Contains("btnupdateuser"))
{
if (!Request["btnupdateuser"].IsEmpty())
{
Name = Request.Form["item.Name"];
password = Request["item.Password"];
EmployeeNumber = Request["item.EmployeeNumber"];
Userteam = Request["item.UserTeam"];

db.UpdateTruckUser(Name, password, EmployeeNumber, Userteam);
model.UserList = db.SearchTruckUsers("");
return PartialView(model);
}
}
ModelState.Clear();
return View(model);
}
model.TriggerOnLoadMessage = ErrorMessage;
return View(model);
}

Controller - SearchPartialView

[HttpPost]
public PartialViewResult SearchPartialView(string searchUserString)
{
if (Session["myID"] == null)
{
ExpireSession();
}

if (!ModelState.IsValid)
RedirectToLogin();

if (searchUserString.Any(char.IsDigit))
{
var model1 = new RegisterModel
{
TriggerOnLoad = false,
UserList = db.SearchTruckUsersEmpNum(searchUserString),
UserTeamList = GetUserTeamList()
};
return PartialView("_RegisterTruckUserPartialView", model1);
}

var model = new RegisterModel
{
TriggerOnLoad = false,
UserList = db.SearchTruckUsers(searchUserString),
UserTeamList = GetUserTeamList()
};
return PartialView("_RegisterTruckUserPartialView", model);
}

注册布局

@model Name.Models.RegisterModel

<!DOCTYPE html>
<html lang="en">
<head>
<title>@ViewBag.Title - Register</title>
@Scripts.Render("~/Scripts/jquery.unobtrusive-ajax.min.js")
</head>

<body>
@RenderPage("~/Views/Shared/_Header.cshtml")
@RenderBody()
<div class="center" id="top">

<div id="search" class="active">
<h1>Manage users</h1>

@using (Ajax.BeginForm("SearchPartialView", "Register", null,
new AjaxOptions
{
HttpMethod = "POST",
InsertionMode = InsertionMode.Replace,
UpdateTargetId = "divUsers",
}, new
{
id = "usertable"
}))
{

@Html.TextBox("SearchUserString", null, new { @class = "responsivetextbox", @placeholder = "Search username or employment number" })
<p>
<input type="submit" class="standardbutton logsearch" name="submit" value="Search" />
</p>
}
<div id="divUsers">
@{Html.RenderPartial("_RegisterTruckUserPartialView");}
</div>
</div>
</div>

@RenderPage("~/Views/Shared/_Footer.cshtml")
</body>
</html>

分部 View (表格)

<table class="centeredTable">
<thead>
<tr>
<th>Name</th>
<th>Password</th>
<th>Employment Number</th>
<th>Team</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.UserList)
{
using (Html.BeginForm())
{
<tr id="usertabletr1">
<td id="usertabletd1">
@Html.EditorFor(modelItem => item.Name, new {htmlAttributes = new {@name = "test1", @style = "border: 0px none;"}})
</td>
<td id="usertabletd1">
@Html.EditorFor(modelItem => item.Password, new {htmlAttributes = new {@name = "registerPassword", @type = "password", @style = "border: 0px none; "}})
</td>
<td id="usertabletd1">
@Html.EditorFor(modelItem => item.EmployeeNumber, new {htmlAttributes = new {@name = "employeeNumber", @readonly = "readonly", @style = "border: 0px none; background: white;"}})
</td>
<td id="usertabletd1">
@Html.DropDownListFor(modelItem => item.UserTeam, Model.UserTeamList, item.UserTeam, new {htmlAttributes = new {@name = "Team", @style = "border: 0px none;"}})
</td>
<td id="usertabletd1">
<input class="standardbutton adddefaultvaluesubmit" type="image" src="~/Pictures/saveicon.ico" alt="Save" value="Save" name="btnupdateuser"/>
<input class="standardbutton adddefaultvaluesubmit" type="image" src="~/Pictures/trashcanicon.ico" alt="Delete" value="Delete" name="btndeleteuser"/>
</td>
</tr>
}
}
</tbody>
</table>

最佳答案

在您的部分 View 中,它应该看起来像这样。

<input type="button" value="Confirm team change" class="btnConfirm"/>

然后将其添加到“主”布局。

$(document).on('click', '.btnConfirm', function(){
$.ajax({
url: '@Url.Action("RegisterTruckUser", "Register")',
type: 'POST',
cache: false,
async: true,
data: $('form').serialize(),
success: function(result){
//do something
}
});
});

关于c# - 部分 View 中的按钮在搜索后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39463314/

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