Asp.net mvc 4 如何使用asp.net mvc将数据插入表

Asp.net mvc 4 如何使用asp.net mvc将数据插入表,asp.net-mvc-4,Asp.net Mvc 4,我是asp.net mvc新手,正在做关于asp.net的项目,我想插入数据,但无法运行此代码,因为参数supplierID为空值(supplierID数据已经在我的表中) 如何插入这些数据 PhoneNo、MobileNo和FaxNo是我要插入到表中的数据 这是我的密码 模型 控制 在控制中,我从表中调用supplierID进行查询。这是一个供应商ID为空的问题 看法 { @Html.HiddenFor(a=>a.SupplierID) @Html.Label(“,T(“电话号码”).ToS

我是asp.net mvc新手,正在做关于asp.net的项目,我想插入数据,但无法运行此代码,因为参数supplierID为空值(supplierID数据已经在我的表中)

如何插入这些数据

PhoneNo、MobileNo和FaxNo是我要插入到表中的数据

这是我的密码

模型

控制 在控制中,我从表中调用supplierID进行查询。这是一个供应商ID为空的问题

看法

{


@Html.HiddenFor(a=>a.SupplierID)
@Html.Label(“,T(“电话号码”).ToString(),新的{@class=“captionStyle1”})
@Html.DevExpress().TextBox(设置=>
{
settings.Name=“PhoneNumBox”;
设置。宽度=170;
settings.Properties.NullText=“输入您的电话号码”;
}
).GetHtml()
@Html.Label(“,T(“手机号码”).ToString(),新的{@class=“captionStyle1”})
@Html.DevExpress().TextBox(
设置=>
{
settings.Name=“MobileNumBox”;
设置。宽度=170;
settings.Properties.NullText=“输入您的手机号码”;
}
).GetHtml()
@Html.Label(“,T(“传真号码”).ToString(),新的{@class=“captionStyle1”})
@Html.DevExpress().TextBox(
设置=>
{
settings.Name=“faxmnumbox”;
设置。宽度=170;
settings.Properties.NullText=“输入传真号码”;
}
).GetHtml()

}

注意大写字母,
SupplierID
SupplierID
不同。您在视图中发送的内容与您在控制器中接收到的内容不同您的错误是此处的public ActionResult ContactCompany(int supplierID)参数int不能为null,您应该更改为public ActionResult ContactCompany(int?supplierID),我将编译您的代码,但是ActionResult中的这个参数int supplierID来自其他任何地方??我想在代码的另一个地方,你有一些类似于@Html.ActionLink(“联系公司”、“联系公司”、“你的控制器”)的东西,或者可能是一个ajaxcall?我不知道,但如果你有它,请确保传递类似以下内容的id
  • @Html.ActionLink(“联系公司”、“联系公司”、“主页”,空,新{supplierID=54})
  • 谢谢你的评论。现在我可以解决这个问题了。我只是为get supplierID创建会话,然后它就可以正常编译了。
     public partial class GeneralInfosModel
    {
        public int SupplierID { get; set; }        
        public string CountryCode { get; set; }
        public string TaxID { get; set; }       
        public string PhoneNo { get; set; }       
        public string MobileNo { get; set; }
        public string FaxNo { get; set; }        
    }
    
           public ActionResult ContactCompany(int supplierID )
        {
           // return View();
            var model = _organizationRepository.GetDataBySupplierID(supplierID );
            return View("ContactCompany", model);
        }
        [HttpPost]
           public ActionResult ContactCompany(Tbl_Organization model)
        {
            var OrgModel = _organizationRepository.GetDataBySupplierID(model.SupplierID);
            if (OrgModel != null)
            {
                OrgModel.PhoneNo = model.PhoneNo;               
                OrgModel.MobileNo = model.MobileNo;                
                OrgModel.FaxNo = model.FaxNo;               
                _organizationRepository.Update(OrgModel);
            }
            var Orgmodel = _organizationRepository.GetDataBySupplierID(model.SupplierID);
            return View("ContactCompany", Orgmodel);
        }
    
    @using (Html.BeginForm("ContactCompany", "ContactInformation", FormMethod.Post, new { id = "SupplierID" }))
    
    <div class="col-sm-8 col-md-8">
        <div class="panel panel-default">
            <div class="panel-body">
                <input type="submit" value="@T("_Global.BtnSave")" class="btn btn-primary" onclick="disablePageForm();" />
                <div style="padding-top:40px;">
                </div>
         @Html.HiddenFor(a => a.SupplierID)                
                <div class="form-inline-element margin-none" style="width: 15%;">
                    @Html.Label("", T("Phone No. ").ToString(), new { @class = "captionStyle1" })
                </div>
                <div>
                    <div class="editorContainer">
    
                        @Html.DevExpress().TextBox( settings =>
                {
                    settings.Name = "PhoneNumBox";
                    settings.Width = 170;
                   settings.Properties.NullText = "Enter your Phone Number";
                }
            ).GetHtml()
                    </div>
                </div>
    
    
    
                <div style="padding-top:40px;">
                </div>
    
                <div class="form-inline-element margin-none" style="width: 15%;">
                    @Html.Label("", T("Mobile Number ").ToString(), new { @class = "captionStyle1" })
                </div>
    
                <div>
                    <div class="editorContainer">
                        @Html.DevExpress().TextBox(
                settings =>
                {
                    settings.Name = "MobileNumBox";
                    settings.Width = 170;
                    settings.Properties.NullText = "Enter your Mobile Number";
                }
            ).GetHtml()
                    </div>
                </div>
    
                <div style="padding-top:40px;">
                </div>
    
                <div class="form-inline-element margin-none" style="width: 15%;">
                    @Html.Label("", T("Fax Number ").ToString(), new { @class = "captionStyle1" })
                </div>
    
                <div>
                    <div class="editorContainer">
                        @Html.DevExpress().TextBox(
                settings =>
                {
                    settings.Name = "FaxNumBox";
                    settings.Width = 170;
                    settings.Properties.NullText = "Enter your Fax Number";
                }
            ).GetHtml()
                    </div>
                </div>
    
    
    
    
            </div>
        </div>
    </div>