Why does ASP.NET MVC Html.CheckBox output two INPUTs with the same name?

后端 未结 7 806
不知归路
不知归路 2020-12-03 17:07

Why in the world does the line:

<%= Html.CheckBox(\"ForSale\", Model.Product.ForSale)%> For Sale

result in the following HTML:

<
7条回答
  •  暖寄归人
    2020-12-03 17:32

    Here's how I've done it in one of my apps. Frustrating, but it seems to work.

    public virtual ActionResult Edit(int id, FormCollection values)
    {
        SomeObject dbData = _repository.GetSomeObject(id);
    
        try
        {
            UpdateModel(dbData);
            if (values.GetValues("CheckBoxId").Contains("true")) 
                dbData.SomeBooleanProperty = true;
            else 
                dbData.SomeBooleanProperty = false;
    
            _repository.Save();
    
            Session["Success"] = "Successfully edited the part.";
            return RedirectToAction("Index");
        }
        catch
        {
            // Handle validation errors here
            return View(new SomeObjectFormViewModel(dbData));
        }
    }
    

    Hope this helps. If you've got any follow-up questions, just leave a comment and I'll update my answer accordingly.

提交回复
热议问题