插入db时始终接收空数据

时间:2016-07-20 10:16:27

标签: c# asp.net-mvc-4

我正在使用MVC 4.我在局部视图中制作它,因为在一种形式中我想一次将大量数据插入到其他数据库中。但是当我插入数据时,它成功插入但我从文本框中获取的数据始终为空,只有已经初始化的数据可以插入。它可以成功插入但数据始终为空。请帮我。谢谢你提前。

模型

namespace admission.Models
{
    public class AlamatRumahModel
    {
        public int id_alamat_rumah { get; set; }
        public string alamat_lengkap { get; set; }
        public string provinsi { get; set; }
        public string kota { get; set; }
        public string kode_pos { get; set; }
        public string telepon_rumah { get; set; }
        public string hp { get; set; }
        public string fax { get; set; }
        public string email { get; set; }
        public DateTime created { get; set; }
        public string createdBy { get; set; }
        public DateTime modified { get; set; }
        public string modifiedBy { get; set; }

    }
}

控制器

[HttpPost]
        public ActionResult IRumah(AlamatRumahModel model)
        {
            Tbl_Alamat_Rumah rumah = new Tbl_Alamat_Rumah();
            model = new AlamatRumahModel();
            using (pmbEntities db = new pmbEntities())
            {

                int LastRumah = db.Tbl_Alamat_Rumah.Max(r => r.id_alamat_rumah);


                rumah.id_alamat_rumah = LastRumah+ 1;
                rumah.alamat_lengkap = model.alamat_lengkap;
                rumah.provinsi= Request["propinsiR"];
                rumah.kota = Request["kotaR"];
                rumah.kode_pos = model.kode_pos;
                rumah.telepon_rumah = model.telepon_rumah;
                rumah.hp = model.hp;
                rumah.fax = model.fax;
                rumah.email = model.email;
                rumah.created = DateTime.Now;
                rumah.modified = DateTime.Now;
                rumah.createdBy = "";
                rumah.modifiedBy = "";

                db.Tbl_Alamat_Rumah.Add(rumah);
                db.SaveChanges();

                return View();

            }

        }


[HttpPost]
        public ActionResult InsertDataDiri(DataDiriModel model, AlamatRumahModel rumah, AlamatSuratModel surat, CpModel cp)
        {
            IDataDiri(model);
            IRumah(rumah);
            ISurat(surat);
            IcP(cp);
            return RedirectToAction("InsertRiwayatPendidikan", "User");

        }

查看

@model admission.Models.AlamatRumahModel

@using (Html.BeginForm("IRumah", "User", FormMethod.Post))
{
    <fieldset>
        <div class="col-xs-12">
            <div class="col-md-12">
                <br><br>
                <legend>
                    <h3><span>Alamat Rumah<i class="arrow-down"></i></span></h3>
                </legend>
                <br>
                <!-- Text input-->
                <div class="content">
                    <div class="form-group">
                        <label class="col-md-4 control-label">Alamat Lengkap</label>
                        <div class="col-md-5">
                            @Html.TextAreaFor(model => model.alamat_lengkap, new { @class = "form-control input-md", @autocomplete = "off"})

                        </div>
                    </div>
                    <br>
                    <br>
                    <br />
                    <!-- Select Basic -->
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="stack_id">Propinsi</label>
                        <div class="col-md-5">
                            @(Html.Kendo().DropDownList()
                                    .Name("propinsiR")
                                    .OptionLabel("Please select Branch ...")
                                    .HtmlAttributes(new { style = "width:100%" })
                                    .DataTextField("Text")
                                    .DataValueField("Value")
                                    .Filter("contains").DataSource(source => { source.Read(read => read.Action("GetProvinsiByJSON", "Lokasi")); })
                            )
                        </div>
                    </div>
                    <br>
                    <br>
                    <!-- Select Basic -->
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="stack_id">Kota</label>
                        <div class="col-md-5">
                            @(Html.Kendo().DropDownList()
                                    .Name("kotaR")
                                    .OptionLabel("Please select Branch ...")
                                    .HtmlAttributes(new { style = "width:100%" })
                                    .DataTextField("Text")
                                    .DataValueField("Value")
                                    .Filter("contains").DataSource(source => { source.Read(read => read.Action("GetKotaByJSON", "Lokasi")); })
                            )
                        </div>
                    </div>
                    <br>
                    <br>
                    <!-- Text input-->
                    <div class="form-group">
                        <label class="col-md-4 control-label" for="cmpny">Kode Pos</label>
                        <div class="col-md-5">
                            @Html.TextBoxFor(model => model.kode_pos, "", new { @class = "form-control input-md", @autocomplete = "off" })

                        </div>
                    </div>
                    <br>
                    <br>

这是我在我的数据库中得到的 enter image description here

1 个答案:

答案 0 :(得分:2)

我刚刚在你的控制器中注意到你再次实例化了这个模型。

[HttpPost]
public ActionResult IRumah(AlamatRumahModel model)
{
    Tbl_Alamat_Rumah rumah = new Tbl_Alamat_Rumah();
    model = new AlamatRumahModel();  //Remove this line of code
    using (pmbEntities db = new pmbEntities())
    {
        ...

这就是你失去模特价值的原因。删除那行代码,它应该可以正常工作。