ASP.NET MVC3 | CheckBoxFor(m => m.Go) is unchecked

2019-08-21 12:30发布

I am extremely confused with what is going on. So Go is a boolean variable and in my Model Go is currently resulting to true. How come these two result in a different value?

@Html.TextBoxFor(m => m.Go); 
@Model.Go;
@Html.CheckBoxFor(m => m.Go);

@Html.TextBoxFor(m => m.Go) Results to FALSE
@Html.CheckBoxFor(m => m.Go) Results to an UNCHECKED box (when i'm expected a checked box)
@Model.Go Results to TRUE (What I expect to happen)

How come @Html.TextBoxFor(m => m.Go) is resulting to false?
and
How come @Html.CheckBoxFor(m => m.Go) is resulting an UNCHECKED box. Thank you very much!

My CONTROLLER

    [HttpPost]
    public ActionResult IXT_FS_LeakCheckAnode(IXT_FS_LeakCheckAnodeModel model, string buttontype)
    {
        model.FormObject = new FormSubmitObject();
        // If the Go has been hit, set the FormObject go be true also.
        if (model.Go) { model.FormObject.go = true; }
        // Add values if necessary, If its not on a submit it will be okay, because it     will not submit in the model.
        List<string> list = new List<string>();
        list.Add("SNT" + model.MainSerial);
        list.Add("USR" + model.BadgeID);
        list.Add("TMS" + 0);

        model.FormObject.MainSerial = model.MainSerial; // Sets the main serial

        model.FormObject = mm.submitGetFormHelper(model.FormObject, "XFA", list, ModelState.IsValid, buttontype);
        if (model.FormObject.checkmsg == "go_pass")       // Update header info.
        {
            model.TubeType = model.FormObject.form_values_dict["TUT"];
            model.FormObject.go = true;
            model.Go = true;
        }
        else if (model.FormObject.checkmsg == "get_pass")  // Update the empty filled.
        {
            model.BadgeID = model.FormObject.form_values_dict["USR"];
            model.MainSerial = model.FormObject.form_values_dict["SNT"];

            // Grabs the TMS double value and converts it into a Time Stamp.
            double latestDate_dbl = Convert.ToDouble(model.FormObject.form_values_dict["TMS"]);
            DateTime latestDate = mm.parseTimeStamp(latestDate_dbl);
            model.LatestDate = "Completed on: " + latestDate.ToString();
            ViewData["Error"] = model.FormObject.errormsg;
        }
        else if (model.FormObject.checkmsg == "submit_pass" || model.FormObject.checkmsg == "clear_pass")// Clear form and let operator submission passed. or clear button
        {
            ModelState.Clear();
            IXT_FS_LeakCheckAnodeModel clear_model = new IXT_FS_LeakCheckAnodeModel();
            return View(clear_model);
        }


        ViewData["Error"] = model.FormObject.errormsg;

        return View(model);
    }

2条回答
Emotional °昔
2楼-- · 2019-08-21 12:50

More than likely, you have something in ViewBag or ViewData called Go, and it's interfering with things. MVC prefers model state over model values when rendering.

查看更多
三岁会撩人
3楼-- · 2019-08-21 13:07

I believe that for a boolean value, you should probably consider using a checkbox instead of a text box. What happens if you try:

@Html.EditorFor(m=>m.Go)
查看更多
登录 后发表回答