C# 将html5数字输入值插入数据库

C# 将html5数字输入值插入数据库,c#,asp.net-mvc,html,C#,Asp.net Mvc,Html,我正试图将HTML5输入控件的值插入db中,但它的值是以null形式插入的。这是我的代码。 视图: 控制器: [httpPost] public ActionResult AddVehicles(AddSpaces adspace) { if (ModelState.IsValid) { string userName = User.Identity.Name;

我正试图将HTML5输入控件的值插入db中,但它的值是以null形式插入的。这是我的代码。 视图:

控制器:

        [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();
           }
        }
     }
[httpPost]
公共行动结果AddVehicles(AddSpaces adspace)
{
if(ModelState.IsValid)
{
字符串userName=User.Identity.Name;
var queryUser=来自会话中的用户。Query()
其中user.email==用户名
选择用户;
if(queryUser.Count()>0)
{
foreach(queryUser中的var updateSpaces)
{
updateSpaces.BPH=adspace.noOfCars;
}
Session.SaveChanges();
}
}
}

我已将模型的noOfCars属性更改为int,但它不起作用。

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

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

第一个参数的工作原理与LabelFor相同,第二个参数是一个annonymous方法,其中包含将作为属性输出到HTML元素的键值对。

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

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

第一个参数的工作原理与LabelFor相同,第二个参数是一个annonymous方法,其中包含将作为属性输出到HTML元素的键值对。

似乎缺少模型作为参数。什么是
adspace
?抱歉,我发布了错误的代码。现在我更新了它。这不起作用,因为模型(
adspac
)为空或只是
noOfCars
?似乎您缺少模型作为参数。什么是
adspace
?抱歉,我发布了错误的代码。现在我更新了它。这不起作用是模型(
adspac
)为空还是仅
noOfCars
 @Html.LabelFor( m => m.noOfCars)
 <input type="number" min="1" max="1000" step="1" name="noOfCars">
    @Html.TextBoxFor(m => m.noOfCars, new { type = "number", min = "1", max = "1000" })