我有一个弱类型的应用程序,我认为最好将其更改为强类型...但是我遇到了一些问题。
我的控制器为我的剃刀视图提供了一个新模型(其中有一个小形式)。我使用textboxfor(model => mymodel.type)并将其发回给我的控制器。我可以看到帖子有所需的值但是一旦它命中我的控制器,模型就是null。
[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>
基本上,通过fiddler,我可以看到帖子包含我需要的值,但控制器的模型为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; }
}
答案 0 :(得分:3)
你相信: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;}
//...
}