2013-10-05 75 views
1

我试图将html 5输入控件的值插入到db.But中,其值是插入为null.Here是我的代码。 查看:将html5数字输入值插入db

@Html.LabelFor(m => m.noOfCars) 
<input type="number" min="1" max="1000" step="1"> 

型号:

public string noOfCars { get; set; } 

控制器:

 [httpPost] 
     public ActionResult AddVehicles(AddSpaces adspace) 
     { 
      if (ModelState.IsValid) 
      { 
      string userName = User.Identity.Name; 
      var queryUser = from user in Session.Query<AddSpaces>() 
          where user.email == userName 
          select user; 

      if (queryUser.Count() > 0) 
      { 
       foreach (var updateSpaces in queryUser) 
       { 
        updateSpaces.BPH = adspace.noOfCars; 
       } 
        Session.SaveChanges(); 
      } 
     } 
    } 

我已经改变了我的模型的noOfCars属性来诠释,但它不工作。

+0

好像你缺少模型作为参数。什么是'广告空间'? –

+0

对不起,我错误地发布了错误的代码。现在我更新了它。那是行不通的 – Wasfa

+0

模型('adspac')为null还是只是'noOfCars'? –

回答

1

您需要命名您的输入字段,以便MVC为您绑定它。

@Html.LabelFor(m => m.noOfCars) 
<input type="number" min="1" max="1000" step="1" name="noOfCars"> 

或者,您可以使用HTML帮助程序来帮助您命名事物。这应该工作

@Html.TextBoxFor(m => m.noOfCars, new { type = "number", min = "1", max = "1000" }) 

第一个参数的工作方式相同的LabelFor,第二个参数是包含键值对,这将是输出属性的HTML元素的annonymous方法。