如何HTTPPost ASP.Net MVC 5的CheckBoxList数据库 [英] How to HTTPPost ASP.Net MVC 5 CheckBoxList to database

查看:519
本文介绍了如何HTTPPost ASP.Net MVC 5的CheckBoxList数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到麻烦值张贴到我的数据库表从的CheckBoxList。这里是我的模型:

I am having trouble getting the values to post to my database table from a checkboxlist. Here are my models:

public partial class SecurityChange_Access
{
    public int ID { get; set; }
    public string Access_Name { get; set; }
    public bool IsSelected { get; set; }
}

public partial class Change
{
    public int ID { get; set; }
    public string Emp_FirstName { get; set; }
    public string Emp_LastName { get; set; }
    public string Emp_Supervisor { get; set; }
    public string Emp_MangerEmail { get; set; }
    public string Emp_MirrorAccount { get; set; }
    public string Security_Change_Access { get; set; }

    public List<SecurityChange_Access> SecurityAccessList { get; set; }

    public bool addAccess { get; set; }
    public IEnumerable<SelectListItem> ChangeRadioList { get; set; }

    //[Required(ErrorMessage = "You must select an option for TestRadio")]
    public String ChangeRadio { get; set; }
}

public class bTest
{
    public int ID { get; set; }
    public string Name { get; set; }
}

下面是HTTPGET和HTTPPost:

Here is the HTTPGet and HTTPPost:

[HttpGet]
public ActionResult Change(Change change)
{   
    List<bTest> list = new List<bTest>();
    list.Add(new bTest() { ID = 1, Name = "Add Access" });
    list.Add(new bTest() { ID = 2, Name = "Remove Access" });
    list.Add(new bTest() { ID = 3, Name = "Change Access" });
    SelectList sl = new SelectList(list, "Name", "Name");
    change.ChangeRadioList = sl;

    List<ActiveDirectory> objadlist = (from data in       objentity.ActiveDirectories select data).ToList();
    ActiveDirectory objAD = new ActiveDirectory();
    objAD.displayName = "";
    objAD.mail = "";
    objAD.Id = 0;
    objadlist.Insert(0, objAD);
    SelectList objmodeldata = new SelectList(objadlist, "displayName", "displayName", 0);
    /*Assign value to model*/
    change.ADList = objmodeldata;

    change.SecurityAccessList = new List<SecurityChange_Access>();
    change.SecurityAccessList = BindSecurity();

    return View(change);
}

public List<SecurityChange_Access> BindSecurity()
{
    List<SecurityChange_Access> _objsecurity = (from data in _entities.SecurityChange_Access select data).ToList();
    return _objsecurity;
}

[HttpPost]
public ActionResult Change([Bind(Include = "ID, Emp_FirstName, Emp_LastName, Emp_Supervisor, Emp_MangerEmail, Emp_MirrorAccount, Security_Change_Access, Security_Access_To_1, Security_Access_To_2, Security_Access_To_3, Security_Access_To_4, Security_Access_To_5, Security_Access_To_6, Security_Access_To_7, Security_Access_To_8, Security_Access_To_9, Security_Access_To_10, Security_Access_To_Other, Security_Access_ADF_Mirror, Security_Access_JDE_Mirror, Security_Access_NetworkDrives_1, Security_Access_NetworkDrives_2, Security_Access_NetworkDrives_3, Security_Access_NetworkDrives_4, Security_Access_NetworkDrives_5, Security_Access_NetworkDrives_6, Security_Access_NetworkDrives_7, Security_Access_NetworkDrives_8, Security_Access_NetworkDrives_9, Security_Access_NetworkDrives_10, Security_Access_NetworkDrives_11, Security_Access_NetworkDrives_12, Security_Access_NetworkDrives_13, Security_Access_NetworkDrives_14, Security_Access_NetworkDrives_15, Security_Access_NetworkDrives_16, Security_Access_NetworkDrives_17, Security_Access_NetworkDrives_18, Security_Access_NetworkDrives_19, Security_Access_NetworkDrives_20, Security_Access_NetworkDrives_21, Security_Access_NetworkDrives_22, Security_Access_NetworkDrives_23, Security_Access_NetworkDrives_24, Security_Access_NetworkDrives_25, Security_Access_NetworkDrives_26, Security_Access_NetworkDrives_27, Security_Access_NetworkDrives_28, Security_Access_NetworkDrives_29, Security_Access_NetworkDrives_30, IT_Equipment_1, IT_Equipment_2, IT_Equipment_3, IT_Equipment_4, IT_Equipment_5, IT_Equipment_6, IT_Equipment_7, IT_Equipment_8, IT_Equipment_9, IT_Software_1, IT_Software_2, IT_Software_3, IT_Software_4, IT_Software_Other, IT_Media_1, IT_Media_2, IT_Media_3, IT_Media_Other, IT_Other_Equipment, Additional_Information")] Change change, int? id)
{
    try
    {
        if (ModelState.IsValid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("SELECTED COUNTRY :- ").AppendLine();
            foreach (var item in change.SecurityAccessList)
            {
                if (item.IsSelected == true)
                {
                    sb.Append(item.Access_Name + ", ").AppendLine();
                    _entities.Changes.Add(change);
                    _entities.SaveChanges();

                    return RedirectToAction("Change");
                }
            }

        }

    }
    catch (System.Data.Entity.Validation.DbEntityValidationException ex)
    {
         foreach (var e in ex.EntityValidationErrors)
        {
            //check the ErrorMessage property
        }
    }
    return View(change);
}

下面是剃刀code:

<table border="0" width="1000" cellpadding="5" cellspacing="5">
    @for (int i = 0; i < Model.SecurityAccessList.Count; i++)
    {
        <tr>
            <td width="200">
                @Html.CheckBoxFor(m => Model.SecurityAccessList[i].IsSelected)
                @Model.SecurityAccessList[i].Access_Name
                @Html.HiddenFor(m => Model.SecurityAccessList[i].ID)
                @Html.HiddenFor(m => Model.SecurityAccessList[i].Access_Name)
            </td>
        </tr>
    }

在我HTTPGET我结合我的名单,但后来我需要张贴在选定值我的列表。我不知道如何去这样做。我也有越来越的检查列表,以便它是逗号分隔的问题。我似乎无法找到任何网络,可以帮助我与此有关。我只是不能似乎能够把数据上传到Security_Change_Access领域。

In my HTTPGET I am binding my list but then I need to post my list on the checked values. I am not sure how to go about doing this. I am also having problems getting the checked list so it is comma separated. I can't seem to find anything online that can help me with this. I just can't seem to be able to post the data to the Security_Change_Access field.

推荐答案

首先,不通过一个复杂的对象作为参数传递给一个GET方法。这是复杂的对象或集合不会绑定(他们会为null)属性,您可能会超出查询字符串限制(崩溃您的应用程序),然后有丑陋的URL它创建。只是初始化一个新的修改中的方法,或通过一个 ID 并获得更改根据 ID

Firstly, do not pass a complex object as a parameter to a GET method. Properties that are complex objects or collections will not bind (they will be null), you may exceed the query string limit (crashing your app) and then there the ugly url it creates. Just initialize a new Change in the method, or pass an ID and get the Change based on the ID.

您认为code看起来不错,虽然我建议你使用的复选框,为 Access_Name 属性隐藏输入关联的标签似乎没有必要

Your view code looks OK although I suggest you use a label associated with the checkbox and the hidden input for the Access_Name property seems unnecessary

@for (int i = 0; i < Model.SecurityAccessList.Count; i++)
{
  @Html.CheckBoxFor(m => m.SecurityAccessList[i].IsSelected)
  @Html.LabelFor(m => m.SecurityAccessList[i].IsSelected, Model.SecurityAccessList[i].Access_Name)
  @Html.HiddenFor(m => m.SecurityAccessList[i].ID)
}

这应该生成带有名称属性的HTML像

which should generate html with name attribute like

<input type=checkbox" name="SecurityAccessList[0].IsSelected" ...>
<input type=checkbox" name="SecurityAccessList[1].IsSelected" ...>

然后,取下(可怕的) [绑定] 属性。默认情况下,所有的属性绑定,这似乎是你想做的事,但实际上已经排除了 SecurityAccessList (我认为 - TL; DR),并包括一个整体很多'属性'甚至不模型中的存在( Security_Access_To_1,Security_Access_To_2,..... )。如果你发现你的自我需要使用 [绑定(包括=..)] ,停止和使用视图模型显示/编辑你所需要的,而不是

Next, remove the (awful) [Bind] attribute. By default, all properties are bound, which seems to be what you want to do, but in fact you have excluded SecurityAccessList (I think - tl;dr ) and included a whole lot of 'properties' that don't even exist in your model (Security_Access_To_1, Security_Access_To_2, .....). If you ever find your self needing to use [Bind(Include="..")], stop and use a view model to display/edit what you need instead.

POST方法应然后正确绑定你与 change.SecurityAccessList 包含集合模式。请注意,我建议您将参数更改为(比方说)模式。在这种情况下它的好,但也有很多问题,对SO有关绑定失败,因为参数的名称相匹配的模型属性的名称。

The POST method should then correctly bind your model with change.SecurityAccessList containing the collection. Note I recommend you change your parameter to (say) model. In this case its OK, but there are plenty of question on SO about binding failing because the name of the parameter match the name of a model property.

旁注:

列表&LT;&BTEST GT;名单=新名单,LT; BTEST&GT;(); 位在那里你创建一个的SelectList 什么似乎一个单选按钮列表中使用似乎很奇怪。选择列表中甚至不适合于使用在任何情况下,你甚至不使用 ID BTEST 属性。这可能只是以及为

The List<bTest> list = new List<bTest>(); bit where you create a SelectList for what seems to be used in a radio button list seems odd. A select list is not even appropriate for that usage and in any case your not even using the ID property of bTest. It may just as well be

SelectList sl = new SelectList(new List<string>() { "Add Access", "Remove Access", "Change Access" });

产生完全相同的结果,但我怀疑一切你需要的是属性为列表&LT;串GT;

下一步的ActiveDirectory objAD =新ActiveDirectory中(); 位在您似乎是插入一个默认或在选择列表中使用的项目也是错误的。再次使用的唯一属性为显示名,但在任何情况下将其删除,并使用 DropDownListFor的过载()接受 optionLabel

Next the ActiveDirectory objAD = new ActiveDirectory(); bit where your seem to be inserting a 'default' or null item for use in a select list is also wrong. Again the only property you use is displayName but in any case delete it and use the overload of DropDownListFor() that accepts optionLabel

@Html.DropDownListFor(m => m.YourProperty, Model.ADList, "-Please select-")

这将选项&LT;选项值&GT;请选择 - &LT; /选项&GT; 从而可以与 [必需] 属性联合使用。

This adds a null option <option value>-Please Select-</option> which can be used in association with the [Required] attribute.

最后,还有的如果(ModelState.IsValid)块,是没有意义的内部code。你甚至不使用的值SB (不知道其连供),然后您尝试添加变化的同一个实例到数据库 IsSelected = TRUE ,除了它只是因为一旦它被添加,你做一个重定向补充一次即可。

Finally there the code inside the if (ModelState.IsValid) block which makes no sense. You don't even use the value of sb (not sure what its even for) and then your attempting to add the same instance of change into the database each time IsSelected=true, except that its only added once because as soon as it is added, you do a redirect.

这篇关于如何HTTPPost ASP.Net MVC 5的CheckBoxList数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆