简体   繁体   English

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

[英]Two equal pieces of code work differently

Working on my project I faced a problem: EF didn't load the data from connected table, but in other practically equal piece of code all is perfect.在处理我的项目时,我遇到了一个问题:EF 没有从连接的表中加载数据,但在其他几乎相同的代码中,一切都是完美的。 Can somebody explain it?有人可以解释一下吗?

Here is the situation:这是情况:

All is good, type.name is loaded:一切都很好,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);
}

Here I run PacientEdit , then it flows to PacientDetails and gives me full anamnesis, including full type with it's name .在这里,我运行PacientEdit ,然后它流向PacientDetails并给我完整的回忆,包括完整的类型及其名称

All is bad, type.name is null:一切都不好,type.name 为空:

In the second case the name of type is null despite all my tries to load it from db.在第二种情况下,尽管我尝试从 db 加载它,但type的名称为null

[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);
}

In this case all data in pacientDetails , including type_ID is loading, but type.name is null在这种情况下, pacientDetails 中的所有数据,包括 type_ID 正在加载,但type.name 为空

Can somebody explain such behavior?有人可以解释这种行为吗?

Used models:使用的型号:

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; }
}

Here is the view for pacientEdit for AssigmentsController:这是 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 />

I guess the problem is in the fact that you're trying to bind Assignment ID and AssignmentType ID using the same name.我想问题在于您尝试使用相同的名称绑定分配 ID 和分配类型 ID。 Create an AssignmentViewModel class (which is anyway a good practice), where you'll have only the ID of member "type" like this:创建一个 AssignmentViewModel 类(无论如何这是一个好习惯),您将只有成员“类型”的 ID,如下所示:

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; }
}

You could also try to modify the hiddenFor type like this:您也可以尝试像这样修改 hiddenFor 类型:

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

Hope this help希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM