【发布时间】:2020-09-16 20:49:30
【问题描述】:
我想在 webgrid 中创建一个提交按钮,它将一行发送到另一个表,但是当用户已经这样做时,我不知道如何禁用此按钮。
第一个表:
CREATE TABLE [dbo].[Applications] (
[ApplicationId] INT IDENTITY (1, 1) NOT NULL,
[ApplicantName] NVARCHAR (50) NOT NULL,
[DepartmentName] NVARCHAR (100) NOT NULL,
[StartDate] DATETIME NOT NULL,
[EndDate] DATETIME NOT NULL,
[AssistantName] NVARCHAR (50) NOT NULL,
[Description] NVARCHAR (MAX) NULL,
[Approved] BIT NULL,
PRIMARY KEY CLUSTERED ([ApplicationId] ASC)
);
我在这里创建了一个“已批准”值,当用户单击按钮时,该值将变为“真”。然后,当 Approved==True 按钮应该被禁用时。
第二张表:
CREATE TABLE [dbo].[Events] (
[EventID] INT IDENTITY (1, 1) NOT NULL,
[Subject] NVARCHAR (100) NOT NULL,
[DepartmentName] NVARCHAR (100) NULL,
[AssistantName] NVARCHAR (50) NULL,
[Description] NVARCHAR (300) NULL,
[Start] DATETIME NOT NULL,
[End] DATETIME NOT NULL,
[ThemeColor] NCHAR (10) NULL,
[IsFullDay] BIT NOT NULL,
PRIMARY KEY CLUSTERED ([EventID] ASC)
);
我对此操作的控制者:
[HttpPost, ValidateInput(false)]
public ActionResult Accept(string applicationJSONaccept, Event model)
{
Application apl = (new JavaScriptSerializer()).Deserialize<Application>(applicationJSONaccept);
MyDatabaseEntities dc = new MyDatabaseEntities();
Application app = dc.Applications.FirstOrDefault(x => x.ApplicationId == apl.ApplicationId);
app.Approved = true;
dc.Events.Add(new Event
{
Subject = apl.ApplicantName,
DepartmentName = apl.DepartmentName,
Description = apl.Description,
Start = apl.StartDate,
End = apl.EndDate,
IsFullDay = false,
AssistantName = apl.AssistantName
});
dc.SaveChanges();
return Redirect("ApplicationView");
}
网络网格:
@grid.Table(
htmlAttributes: new { @id = "grid", @class = "Grid" },
tableStyle: "table table-responsive table-bordered",
columns: grid.Columns(
grid.Column(columnName: "ApplicationId", header: "Nr"),
grid.Column(columnName: "ApplicantName", header: "Imię i nazwisko"),
grid.Column(columnName: "DepartmentName", header: "Oddział"),
grid.Column(columnName: "StartDate", header: "Od"),
grid.Column(columnName: "EndDate", header: "Do"),
grid.Column(columnName: "AssistantName", header: "Zastępca"),
grid.Column(columnName: "Description", header: "Opis"),
//Here is this button:
grid.Column(null, header: "", format: @<text>@Html.ActionLink("Zatwierdź", null, null, new { @class = "accept btn btn-success", @role="button" })</text>)
))
@using (Html.BeginForm("Accept", "Home", FormMethod.Post, new { @id = "IndexForm2" }))
{
<input type="hidden" name="applicationJSONaccept" />
}
JS:
<script type="text/javascript">
$("body").on("click", ".accept", function () {
var row = $(this).closest("tr");
var application = {};
application.ApplicationId = row.find("td").eq(0).html();
application.ApplicantName = row.find("td").eq(1).html();
application.DepartmentName = row.find("td").eq(2).html();
application.StartDate = new Date(row.find("td").eq(3).html().substr(6, 4), row.find("td").eq(3).html().substr(4, 2) - 1, row.find("td").eq(3).html().substr(0, 2));
application.EndDate = new Date(row.find("td").eq(4).html().substr(6, 4), row.find("td").eq(4).html().substr(4, 2) - 1, row.find("td").eq(4).html().substr(0, 2));
application.AssistantName = row.find("td").eq(5).html();
application.Description = row.find("td").eq(6).html();
$("[name=applicationJSONaccept]").val(JSON.stringify(application));
$("#IndexForm2")[0].submit();
return false;
});
</script>
一切正常,行转到另一个表,Approved 的值正在改变,但是当 Approved==false 时如何禁用此按钮时,我遇到了问题。你知道我该怎么做吗? 提前致谢!
编辑: 试过了,还是不行。
grid.Column(null, header: "", format:
@<text>
@if (ViewBag.Approved == false || ViewBag.Approved == null) {
@Html.ActionLink("Zatwierdź", null, null, new { @class = "accept btn btn-success", @role = "button" })
}
else
{
@Html.ActionLink("Zatwierdź", null, null, new { @class = "accept btn btn-success disabled", @role = "button" });
}
</text>)
控制器更新:
[HttpPost, ValidateInput(false)]
public ActionResult Accept(string applicationJSONaccept, Event model)
{
Application apl = (new JavaScriptSerializer()).Deserialize<Application>(applicationJSONaccept);
MyDatabaseEntities dc = new MyDatabaseEntities();
Application app = dc.Applications.FirstOrDefault(x => x.ApplicationId == apl.ApplicationId);
ViewBag.Approved = app.Approved;
if (app.Approved == false || app.Approved == null)
{
app.Approved = true;
dc.Events.Add(new Event
{
Subject = apl.ApplicantName,
DepartmentName = apl.DepartmentName,
Description = apl.Description,
Start = apl.StartDate,
End = apl.EndDate,
IsFullDay = false,
AssistantName = apl.AssistantName
});
dc.SaveChanges();
}
else
{
}
return Redirect("ApplicationView");
}
【问题讨论】:
标签: asp.net asp.net-mvc