无法从数据库的下拉列表中删除项目。使用MVC

时间:2018-07-16 14:32:25

标签: asp.net-mvc sql-delete html.dropdownlistfor

在我的程序中,管理员可以删除下拉列表中的管理器。单击按钮时,我无法通过实际值。当前,它传递一个“空”值。我希望在单击按钮时从数据库中删除该值。

我的看法

<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");
    }

2 个答案:

答案 0 :(得分:0)

给出一个与您在JQuery中使用的ID匹配的ID值:

htmlAttributes: new { @class = "form-control", @id = "selectedManager" })

您是JQuery代码:

manager: $('#selectedManager').val() //Changed this to accept the same named parameter as server side

答案 1 :(得分:0)

您的参数不匹配。您的控制器需要manager,而您正在传递selectedManager。试试这个。

$.post("/Settings/DeleteManager",
{
   manager: $('#selectedManager').val()
},
function (data) {
   if (data == "True") {}  
   else {}            
});