c#-即使修复了模型定义中的设置后,控制器中的模型也为空

我的应用程序的类型很弱,因此我决定最好将其更改为强类型…但是我遇到了一些问题.

我的控制器将新模型提供给我的剃刀视图(其中包含一个小表格).我使用textboxfor(model => mymodel.type)并将其发布回我的控制器.我可以看到该帖子具有所需的值,但是一旦它到达我的控制器,模型就为空.

 [HttpGet]
        public ActionResult LogIn(int Op)
        {
                    LoginWrapperClass newOperation = new LoginWrapperClass();
                    newOperation.newL = new ePSDB();
                    newOperation.newP = new eP(Op, Request.Cookies["State"].Value);
                    return View(newOperation);
            }

  [HttpPost, ValidateInput(true)]
        public ActionResult LogIn(LoginWrapperClass newOperation)
        {
            if (ModelState.IsValid)
            {
                newOperation.LookUp();
                if (newOperation.newP != null)
                {
                    //stuff

                    return RedirectToAction("RecordOp", "Authenticated", newOperation.newP);
                }
            }

视图:

@model WebApp.Models.LoginWrapperClass

@{
    ViewBag.Title = "Operation";
}

<div id="indexOp" class="textCenter">
    <div id="time"></div>
    <div id="loader" class="hiddenAndGone"><img src="~/Content/Images/loader.gif"></div>
    <div id="operations">
        @using (Html.BeginForm())
        {
            //Html.AntiForgeryToken();
            <div class="Ops">
                <div>
                    @Html.TextBoxFor(model => model.newL.BADGE_NBR, new { @id = "BADGE_NBR", @class = "readFont", autofocus = "autofocus", autocomplete = "off", placeholder = "" })
                    @Html.TextBoxFor(model => model.newP.PUNCH_TYPE, new { type="number", @class = "hidden" })
                </div>
                <div id="submit" class="hidden">
                    <input type="submit" value="validate" class="validateSubmit"  />
                </div>
            </div>
        }
    </div>
</div>

基本上,通过提琴手,我可以看到该帖子包含了我需要的值,但控制器的模型为null.为什么?

我发布的是这样的:
newL.BADGE_NBR = 18845733& newP.PUNCH_TYPE = 1

我在这里添加类声明:

 public class LoginWrapperClass
    {
        public ePSDB newL { get; set; } //fixed per Aaron's response... still no binding
        public eP newP{ get; set; }
        public void LookUp()
        {
            using (var db = new PSTestEntities())
            {
                IEnumerable<ePSDB> foundInDb;
                foundInDb = db.ePSDBs.Where(foundE => this.newL.BADGE_NBR.ToString() == foundE.BADGE_NBR.ToString().Substring(3));

                if (foundInDb.Count() > 0)
                {

                    this.newL = foundInDb.First();
                    this.newP.BADGE_NBR = this.newL.BADGE_NBR;
                    this.newP.EMPLID = this.newL.EMPLID;
                }
            }
        }
    }
//This is a partial class that will be added to but as of now this is all
    public partial class ePSDB
        {
            [Key]
            public int P_Key { get; set; }
            public int EMPLID { get; set; }
            [Required]
            public long BADGE_NBR { get; set; }
            public Nullable<System.DateTime> BIRTHDATE { get; set; }
            public string NAME { get; set; }
        }

    public partial class eP
    {
        [Required]
        public long BADGE_NBR { get; set; }
        [Required]
        public int EMPLID { get; set; }
        [Required]
        public System.DateTimeOffset PUNCH_DTTM { get; set; }
        [Required]
        public int PUNCH_TYPE { get; set; }
        public Nullable<double> TL_QUANTITY { get; set; }
        [Key]
        public long P_Key { get; set; }
    }
 public partial class eP
    {
        [NotMapped]
        public bool containsQuestions = false;
        [NotMapped]
        public List<Question> questions { get; set; }
        public eP(int punchType, string state)
        {
            PUNCH_DTTM = DateTimeOffset.UtcNow;
            PUNCH_TYPE = punchType;
            if((punchType == 1 || punchType == 2) & containsQuestions)
                questions = getQuestions(state, punchType);
        }
    private List<Question> getQuestions(string state, int punchType){
        List<Question> Questions = new List<Question>();
      //do stuff
      return Questions
     }
        ///plus other methods not used in this controller/view
    }
public class Question
    {
        public string QuestionName { get; set; }
        public string QuestionString { get; set; }
        public int Answer { get; set; }
    }

解决方法:

我相信您正在遭受这个痛苦:ASP.net MVC – Model binding excludes class fields?

您可以使用以下字段进行声明:

public class LoginWrapperClass
{
    public ePSDB newL;
    public eP newP;
    //...
}

但是您需要使用属性:

public class LoginWrapperClass
{
    public ePSDB newL {get;set;}
    public eP newP {get;set;}
    //...
}
上一篇:c#-有条件地在html.RadioButtonFor(MVC4 / Razor)中包含选中的属性


下一篇:javascript-如何使用jQuery重置我的Ajax.Begin表单字段值