从来自数据库的下拉列表中删除项目时遇到问题。使用 MVC
Having trouble deleting an item from a drop down list that comes from the database. Using MVC
在我的程序中,管理员可以删除下拉列表中的经理。单击按钮时,我无法获得要传递的实际值。目前它传递一个 'null' 值。单击按钮时,我希望从数据库中删除该值。
我的观点
<div id="addManagerBox" class="admin-home-container-settings boxlike">
<div class="admin-home-itm-settings thinner">
<h2>Add Project Manager</h2>
@Html.TextBoxFor(m => m.manager, new {@class = "form-control" })
<a id="btnAddManager" class="btn btn-lg btn-primary margintop">Add Manager</a>
</div>
<div class="admin-home-itm-settings thinner">
<h2>Delete Project Manager</h2>
@Html.DropDownListFor(m => Model.selectedManager, Model.AllManagers,"--Select Project Manager--", htmlAttributes: new { @class = "form-control" })
<button id="btnDeleteManager" class="btn btn-lg btn-danger margintop">Delete Manager</button>
</div>
AJAX 代码
$("#btnDeleteManager").click(function () {
$.post("/Settings/DeleteManager",
{
selectedManager: $('#selectedManager').val()
},
function (data) {
if (data == "True") {
} else {
}
});
});
我的模型视图
public class AppSettingsViewModel
{
public string manager { get; set; }
public string purpose { get; set; }
[Required(ErrorMessage = "Email is required")]
[Display(Name = "RegisterEmail")]
public string registerEmail { get; set; }
[Required(ErrorMessage = "Project Manager is required")]
[Display(Name = "Manager")]
public string selectedManager { get; set; }
public List<Managers> allManagers { get; set; }
public List<SelectListItem> AllManagers { get; set; }
[Required(ErrorMessage = "Purpose is required")]
[Display(Name = "Purpose")]
public string selectedPurpose { get; set; }
public List<Purposes> allPurposes { get; set; }
public List<SelectListItem> AllPurposes { get; set; }
[Required(ErrorMessage = "Image History is required")]
[Display(Name = "ImageHistory")]
public string ImageHistory { get; set; }
public List<Questions> allQuestions { get; set; }
public List<Settings> allSettings { get; set; }
public AppSettingsViewModel()
{
allSettings = new List<Settings>();
allPurposes = new List<Purposes>();
allManagers = new List<Managers>();
allQuestions = new List<Questions>();
}
}
控制者
[HttpPost]
public bool DeleteManager(string manager)
{
var managerRepo = new SiteContentRepository();
return managerRepo.EditManager(manager, "delete");
}
为您的下拉菜单提供一个与您在 JQuery 中使用的内容相匹配的 ID 值:
htmlAttributes: new { @class = "form-control", @id = "selectedManager" })
您是 JQuery 代码:
manager: $('#selectedManager').val() //Changed this to accept the same named parameter as server side
您的参数不匹配。您的控制器需要 manager
而您正在通过 selectedManager
。试试这个。
$.post("/Settings/DeleteManager",
{
manager: $('#selectedManager').val()
},
function (data) {
if (data == "True") {}
else {}
});
在我的程序中,管理员可以删除下拉列表中的经理。单击按钮时,我无法获得要传递的实际值。目前它传递一个 'null' 值。单击按钮时,我希望从数据库中删除该值。
我的观点
<div id="addManagerBox" class="admin-home-container-settings boxlike">
<div class="admin-home-itm-settings thinner">
<h2>Add Project Manager</h2>
@Html.TextBoxFor(m => m.manager, new {@class = "form-control" })
<a id="btnAddManager" class="btn btn-lg btn-primary margintop">Add Manager</a>
</div>
<div class="admin-home-itm-settings thinner">
<h2>Delete Project Manager</h2>
@Html.DropDownListFor(m => Model.selectedManager, Model.AllManagers,"--Select Project Manager--", htmlAttributes: new { @class = "form-control" })
<button id="btnDeleteManager" class="btn btn-lg btn-danger margintop">Delete Manager</button>
</div>
AJAX 代码
$("#btnDeleteManager").click(function () {
$.post("/Settings/DeleteManager",
{
selectedManager: $('#selectedManager').val()
},
function (data) {
if (data == "True") {
} else {
}
});
});
我的模型视图
public class AppSettingsViewModel
{
public string manager { get; set; }
public string purpose { get; set; }
[Required(ErrorMessage = "Email is required")]
[Display(Name = "RegisterEmail")]
public string registerEmail { get; set; }
[Required(ErrorMessage = "Project Manager is required")]
[Display(Name = "Manager")]
public string selectedManager { get; set; }
public List<Managers> allManagers { get; set; }
public List<SelectListItem> AllManagers { get; set; }
[Required(ErrorMessage = "Purpose is required")]
[Display(Name = "Purpose")]
public string selectedPurpose { get; set; }
public List<Purposes> allPurposes { get; set; }
public List<SelectListItem> AllPurposes { get; set; }
[Required(ErrorMessage = "Image History is required")]
[Display(Name = "ImageHistory")]
public string ImageHistory { get; set; }
public List<Questions> allQuestions { get; set; }
public List<Settings> allSettings { get; set; }
public AppSettingsViewModel()
{
allSettings = new List<Settings>();
allPurposes = new List<Purposes>();
allManagers = new List<Managers>();
allQuestions = new List<Questions>();
}
}
控制者
[HttpPost]
public bool DeleteManager(string manager)
{
var managerRepo = new SiteContentRepository();
return managerRepo.EditManager(manager, "delete");
}
为您的下拉菜单提供一个与您在 JQuery 中使用的内容相匹配的 ID 值:
htmlAttributes: new { @class = "form-control", @id = "selectedManager" })
您是 JQuery 代码:
manager: $('#selectedManager').val() //Changed this to accept the same named parameter as server side
您的参数不匹配。您的控制器需要 manager
而您正在通过 selectedManager
。试试这个。
$.post("/Settings/DeleteManager",
{
manager: $('#selectedManager').val()
},
function (data) {
if (data == "True") {}
else {}
});