提交表单时无法获取外键值

时间:2019-05-22 10:15:00

标签: c# sql-server entity-framework asp.net-core

我目前在我的ASP.NET Core项目中工作。我创建了一些我映射到数据库表的类。

进阶班:

public class Errand : IDbEntity
{
    public int Id { get; set; }
    public int PANumber { get; set; }
    public string Description { get; set; }
    public string Issuer { get; set; }
    public string Stakeholders { get; set; }
    public string Background { get; set; }
    public string DescriptionOfMission { get; set; }
    public string AcceptanceCriteria { get; set; }
    public string TestObject { get; set; }
    public DateTime AvailableFrom { get; set; }
    public DateTime TestStart { get; set; }
    public DateTime TestDelivery { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }

    public int CoreId { get; set; }
    public virtual Core Core { get; set; }

    public int MachineTypeId { get; set; }
    public virtual MachineType MachineType { get; set; }

    public int ProjectNrId { get; set; }
    public virtual ProjectNumber ProjectNr { get; set; }

    public int TestTypeId { get; set; }
    public virtual Group TestType { get; set; }

    public virtual DBErrandStatus status { get; set; }
}

其他类看起来彼此相同。 这是Core的类:

public class Core : IDbEntity
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }

    public virtual ICollection<Errand> Errands { get; set; }
}

我已经迁移到数据库,并且一切正常,并且Errand表包含Core, Group, MachineType & ProjectNumber的外键。

现在是我的问题。我正在尝试创建A Errand,并且在视图中用户将为差事选择值的视图中,我对外键进行了下拉。这是此代码:

 <form method="post" asp-controller="Home" asp-action="ExecuteCreateErrand">
    <div class="row">
        <div class="col-md-2">
            <label>PA number:</label>
            <input asp-for="@Model.errand.PANumber" class="form-input" type="text" />
        </div>
        <div class="col-md-4">
            <label>Title:</label>
            <input asp-for="@Model.errand.Description" class="form-input" placeholder="Title" type="text" />
        </div>
        <div class="col-md-2">
            <label>Core:</label>
            <select asp-for="@Model.errand.Core" class="form-input">
                @foreach(var core in Model.Core)
                {
                    <option>@core.Name</option>
                }
            </select>
        </div>
        <div class="col-md-4">
            <label>Project Nr:</label>
            <select asp-for="@Model.errand.ProjectNr" class="form-input">
                @foreach(var projNr in Model.ProjectNumber)
                {
                    <option>@projNr.Name</option>
                }
            </select>
        </div>
    </div>
    <div class="row">
        <div class="col-md-2">
            <label>Machine type:</label>
            <select asp-for="@Model.errand.MachineType" class="form-input">
                @foreach (var MT in Model.MachineType)
                {
                    <option>@MT.Name</option>
                }
            </select>
        </div>
        <div class="col-md-4">
            <label>Issuer:</label>
            <input asp-for="@Model.errand.Issuer" class="form-input" value="@Model.User.Username" type="text" readonly />
        </div>
        <div class="col-md-2">
            <label>Test type</label>
            <select asp-for="@Model.errand.TestType" class="form-input">
                @foreach (var TestType in Model.TestType)
                {
                    <option>@TestType.Name</option>
                }
            </select>
        </div>
        <div class="col-md-4">
            <label>Stakeholders</label>
            <input class="form-input" asp-for="@Model.errand.Stakeholders" placeholder="Stakeholders" type="text" />
        </div>
    </div>
    <div class="row">
        <div id="sandbox-container" class=" span5 col-md-4">
            <label>Testobject avalible from:</label>
            <input asp-for="@Model.errand.AvailableFrom" class="form-input" type="text" value="@DateTime.Now.ToString("yyyy/MM/dd")" />
        </div>

        <div id="sandbox-container" class=" span5 col-md-4">
            <label>Desired start of test:</label>
            <input asp-for="@Model.errand.TestStart" class="form-input" type="text" value="@DateTime.Now.ToString("yyyy/MM/dd")" />
        </div>

        <div id="sandbox-container" class=" span5 col-md-4">
            <label>Desired end of test:</label>
            <input asp-for="@Model.errand.TestDelivery" class="form-input" type="text" value="@DateTime.Now.ToString("yyyy/MM/dd")" />
        </div>
    </div>
    <div class="row">
        <div class="col-md-10"></div>
        <div class="col-md-2">
            <button type="submit" class="btn btn-lg btn-block mt-4 vBtn">Create Errand</button>
        </div>
    </div>
</form>

但是当我稍后在提交表单后在运行的动作上设置断点时,我得到了Errand.Core = nullErrand.CoreId = 0

以下是该操作的代码:

public IActionResult ExecuteCreateErrand([FromForm] Errand errand)
    {
        var SQL = new SQLErrandStorage();
        var handler = new ErrandHandler(SQL);
        handler.Create(errand);

        return RedirectToAction("RenderAllErrandsView", "Home");
    }

[FromForm] Errand errand返回此

  

Core = null和CoreId = 0

Action调用此方法:

public void Create(Errand errand)
{
     errandStorage.CreateErrand(errand);
}

依次调用此创建差事的方法:

public void CreateErrand(Errand errand)
    {
        using (var db = new PADBContext())
        {
            db.Errand.Add(errand);
            db.SaveChanges();
        }
    }

我的问题是如何使外键附加在提交的表单中。如何将值Core = null and CoreId = 0更改为我在下拉菜单中选择的值?

我希望这足以说明我的问题。我真的很感谢您的帮助或反馈。预先感谢!

1 个答案:

答案 0 :(得分:1)

问题似乎是asp-for标记帮助器中select值的错误绑定。Select Tag Helper asp-for指定select元素的模型属性名称,而不是模型。

如果您想获得Errand.CoreId,则可以像下面这样设置select tag helper的asp-for

<label>Core:</label>
<select asp-for="@Model.errand.CoreId" class="form-input">
      @foreach (var core in Model.Core)
       {
         <option value="@core.Id">@core.Name</option>
       }
 </select>

如果您想获得Errand.Core,则可以像下面这样设置select标签助手的asp-for:

<label>Core:</label>
<select asp-for="@Model.errand.Core.Id" class="form-input">
      @foreach (var core in Model.Core)
       {
         <option value="@core.Id">@core.Name</option>
       }
 </select>