2种方法一种观点

时间:2015-06-12 11:28:39

标签: c# asp.net entity-framework-5

我试图在一个表单中更改两个类的值但值。但是在我的控制器中只有一个值发生了变化,testname值也是空的。

我正在使用

ViewBag.NameId = new SelectList(db.Names, "NameId", "testname", date.NameId);

有一些想法:)

模特:

public class date
{
              public date() { dateId = Guid.NewGuid(); }

            public Guid dateId { get; set; }
    [Display(Name = "Date de Naissance")]

    [DisplayFormat(DataFormatString = "{0:d}")]
            public string datepicker { get; set; }


    public int NameId { get; set; }

    public virtual Name Name { get; set; }

}

第二课:

public class Name
{

    public int NameId { get; set; }
    public string testname { get; set; }
}

控制器:

    // POST: date/Edit/5
    // Afin de déjouer les attaques par sur-validation, activez les propriétés spécifiques que vous voulez lier. Pour 
    // plus de détails, voir  http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "dateId,datepicker,NameId, testname")] date date)
    {
        if (ModelState.IsValid)
        {
            var a = db.Names.Find(date.NameId);
            a.testname = @date.Name.testname;

            //db.Entry(date).State = EntityState.Modified;
            //db.Entry(date.Name).State = EntityState.Modified;

            db.SaveChanges();

观点:

@using (Html.BeginForm()){
    @Html.AntiForgeryToken()

    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
    @Html.HiddenFor(model => model.dateId)
    @Html.HiddenFor(model => model.NameId)


        @Html.LabelFor(model => model.datepicker, htmlAttributes: new { @class = "control-label col-md-2" })

            @Html.EditorFor(model => model.datepicker, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.datepicker, "", new { @class = "text-danger" })



        @Html.LabelFor(model => model.Name.testname, htmlAttributes: new {@class = "control-label col-md-2" })
        @Html.EditorFor(model => model.Name.testname, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Name.testname, "", new { @class = "text-danger" })

    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Save" class="btn btn-default" />
        </div>
    </div>
</div>

0 个答案:

没有答案