两个相同的代码片段工作方式不同

时间:2015-06-20 23:31:36

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

在我的项目上工作我遇到了一个问题:EF没有加载来自连接表的数据,但在其他几乎相同的代码中,一切都很完美。有人可以解释一下吗?

情况如下:

一切都很好,已加载type.name:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult pacientEdit([Bind(Include = "ID,comments")] Anamnesis anamnesis)
{
    if (ModelState.IsValid)
    {
        db.Entry(anamnesis).State = EntityState.Modified;
        db.SaveChanges();
        return pacientDetails(anamnesis.ID);
    }
    return PartialView(anamnesis);
}

public ActionResult pacientDetails(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Anamnesis anamnesis = db.anamneses.Include(p => p.type).Where(p => p.ID == id).First();
    if (anamnesis == null)
    {
        return HttpNotFound();
    }
    return PartialView("~/views/Anamnesis/pacientDetails.cshtml", anamnesis);
}

在这里我运行 PacientEdit ,然后它流向 PacientDetails 并给我完整的回忆,包括完整的类型及其名称< /强>

一切都很糟糕,type.name为null:

在第二种情况下, type 的名称是 null ,尽管我尝试从db加载它。

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult pacientEdit(Assigment assigment)
{
    if (ModelState.IsValid)
    {
        db.Entry(assigment).State = EntityState.Modified;

        db.SaveChanges();
        return pacientDetails(assigment.ID);
        //dirty fix, but works: return (new AssigmentsController()).pacientDetails(assigment.ID);
    }
    return PartialView(assigment);
}
public ActionResult pacientDetails(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Assigment assigment = db.assigments.Include(p => p.type).Where(p => p.ID == id).First();
    if (assigment == null)
    {
        return HttpNotFound();
    }
    return PartialView("~/views/Assigments/pacientDetails.cshtml", assigment);
}

在这种情况下, pacientDetails 中的所有数据(包括type_ID)都在加载,但 type.name为null

有人可以解释这种行为吗?

二手车型:

public class Anamnesis
{
    public int ID { get; set; }
    public AnamnesisEventType type { get; set; }
    public String comments { get; set; }
}

public class Assigment
{
    public int ID { get; set; }    
    public AssigmentType type { get; set; }    
    public decimal? weight { get; set; }
    public decimal? dose { get; set; }
    public decimal? inADay { get; set; }
    [DataType(DataType.MultilineText)]
    public String comments { get; set; }
    public String medicine { get; set; }
    [DefaultValue(1)]
    public int actual { get; set; }
    public DateTime cancelDate { get; set; }



}
public class AnamnesisEventType
{
    public int ID { get; set; }
    public String name { get; set; }
}
public class AssigmentType
{
    public int ID { get; set; }
    public String name { get; set; }
    public String description { get; set; }
}

以下是针对AssigmentsController的pacientEdit的视图:

@model WebApplication2.Models.Assigment

<form id="@String.Format("AssigmentsEdit{0}", Model.ID)">

    @Html.AntiForgeryToken()
    @Html.HiddenFor(model => model.ID)
    @Html.HiddenFor(model => model.type.ID)
    @Html.HiddenFor(model => model.cancelDate)
    @Html.HiddenFor(model => model.actual)
    <div class="row">
        <div class="col-md-4">
            <strong>
                @Html.DisplayFor(model => model.type.name)
            </strong>
        </div>
        <div class="col-md-6">
            <p>
                @Html.EditorFor(model => model.medicine, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.medicine) } })
            </p>
        </div>
        <div class="col-md-2">
            <a onclick="CancelEdit('Assigments', @Model.ID);" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-backward" aria-hidden="true"></span></a>
            <a class="btn btn-sm btn-primary" onclick="PostEditForm('Assigments', @Model.ID);">
                <span class="glyphicon glyphicon-save" aria-hidden="true"></span>
            </a>
        </div>
    </div>
    <div class="row alert alert-info" style="margin-top:10px">
        <div class="col-md-4">
            <h4><span class="glyphicon glyphicon-tint" aria-hidden="true"></span> Назначение:</h4>
             @Html.EditorFor(model => model.weight, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.weight) } })

            @Html.EditorFor(model => model.dose, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.dose) } }) 

            @Html.EditorFor(model => model.inADay, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.inADay) } })

        </div>

        <div class="col-md-8">
            <h4><span class="glyphicon glyphicon-comment" aria-hidden="true"></span> @Html.DisplayNameFor(model => model.comments):</h4>
            <p>@Html.EditorFor(model => model.comments, new { htmlAttributes = new { @class = "form-control", @placeholder = Html.DisplayNameFor(model => model.comments) } })</p>
        </div>



    </div>

    </form>
        <hr />

1 个答案:

答案 0 :(得分:0)

我想问题在于您尝试使用相同的名称绑定Assignment ID和AssignmentType ID。 创建一个AssignmentViewModel类(这是一个很好的做法),你只有成员的ID&#34;键入&#34;像这样:

public class AssigmentViewModel
{
    public int ID { get; set; }    
    public int TypeId { get; set; }    
    public decimal? weight { get; set; }
    public decimal? dose { get; set; }
    public decimal? inADay { get; set; }
    [DataType(DataType.MultilineText)]
    public String comments { get; set; }
    public String medicine { get; set; }
    public int actual { get; set; }
    public DateTime cancelDate { get; set; }
}

您也可以尝试修改hiddenFor类型:

@Html.HiddenFor(model => model.type)

希望这个帮助