Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 编辑/创建带有外键的EF对象_C#_Asp.net Mvc_Entity Framework_Model Binding - Fatal编程技术网

C# 编辑/创建带有外键的EF对象

C# 编辑/创建带有外键的EF对象,c#,asp.net-mvc,entity-framework,model-binding,C#,Asp.net Mvc,Entity Framework,Model Binding,我对使用ASP.NET MVC和实体框架时处理外键关系的最佳方法感兴趣 我目前正在使用ViewModel输出一个创建和编辑页面(使用partial),但当我将数据发回时,情况并不太好 在验证模型之前,我使用“选择”列表中的“发布”值查找外部对象并将其分配给我的模型,但当我在“编辑”上使用UpdateModel时,引用最终为空,我猜测是因为它无法正确绑定该属性 人们通常如何处理这个问题?使用ViewModel填充我的下拉列表似乎已经足够简单了,但在编辑时我肯定遗漏了一些东西。人们通常创建自己的活页

我对使用ASP.NET MVC和实体框架时处理外键关系的最佳方法感兴趣

我目前正在使用ViewModel输出一个创建和编辑页面(使用partial),但当我将数据发回时,情况并不太好

在验证模型之前,我使用“选择”列表中的“发布”值查找外部对象并将其分配给我的模型,但当我在“编辑”上使用
UpdateModel
时,引用最终为空,我猜测是因为它无法正确绑定该属性

人们通常如何处理这个问题?使用ViewModel填充我的下拉列表似乎已经足够简单了,但在编辑时我肯定遗漏了一些东西。人们通常创建自己的活页夹来解决这个问题吗

我尝试过使用强类型和
FormCollection

视图模型:

public class ReportViewModel
 {
    public Report Report { get; set; }
    public SelectList ReportDeliveryMethods { get; set; }
    public string ReportDeliveryMethod { get; set; }
    public SelectList ReportReceivers { get; set; }
    public string ReportReceiver { get; set; }
    public SelectList PermitTypes { get; set; }
    public string PermitType { get; set; }
}
控制器:

[HttpPost]        
public ActionResult Edit(int id, FormCollection collection)
        {
            Report report;

            try
            {
                report = repository.GetReport(id);

                // Convert ReportDeliveryMethod to Object Reference                        
                if (!string.IsNullOrEmpty(collection["ReportDeliveryMethod"]))
                {
                    int reportDeliveryMethodId = 0;
                    if (int.TryParse(collection["ReportDeliveryMethod"], out reportDeliveryMethodId))
                    {
                        report.ReportDeliveryMethod = repository.GetReportDeliveryMethod(reportDeliveryMethodId);
                    }
                }

                // Convert ReportReceiver to Object Reference              
                if (!string.IsNullOrEmpty(collection["ReportReceiver"]))
                {
                    int reportReceiverId = 0;
                    if (int.TryParse(collection["ReportReceiver"], out reportReceiverId))
                    {
                        report.ReportReceiver = repository.GetReportReceiver(reportReceiverId);
                    }
                }

                // Convert PermitType to Object Reference              
                if (!string.IsNullOrEmpty(collection["PermitType"]))
                {
                    int permitTypeId = 0;
                    if (int.TryParse(collection["PermitType"], out permitTypeId))
                    {
                        report.PermitType = repository.GetPermitType(permitTypeId);
                    }
                }

                if (ModelState.IsValid)
                {
                    UpdateModel(report);
                    repository.Save();

                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch (Exception ex)
            {
                return View();
            }
        }
表格:


领域
model.Report.ShareName)%>
model.Report.ShareName)%>
model.Report.ShareName)%>
型号.报告.说明)%>
型号.报告.说明)%>
型号.报告.说明)%>
型号.报告.频率)%>
型号.报告.频率)%>
型号.报告.频率)%>
model.Report.SendTime)%>
model.Report.SendTime)%>
model.Report.SendTime)%>
model.Report.ReportDeliveryMethod)%>
model.ReportDeliveryMethods,model.ReportDeliveryMethods)%>
model.Report.ReportDeliveryMethod)%>
model.Report.ReportReceiver)%>
model.ReportReceiver,model.ReportReceivers)%>
model.Report.ReportReceiver)%>
model.Report.PermitType)%%>
model.PermitType,model.PermitType)%%>
model.Report.PermitType)%%>


<代码> > p>让我们考虑<代码> RePvices方法>代码>。在视图模型中,它是一个
字符串
。在您的
报表
对象上,它是
ReportDeliveryMethod
类型。由于
字符串
不能隐式转换为
ReportDeliveryMethod
UpdateModel
不会绑定它

那么你的选择是什么

  • 手动绘制地图,就像您现在所做的那样
  • 绑定ID,而不是对象引用。EF4支持。您可以在视图模型中放置
    ReportDeliveryMethodId
    ,而不是
    ReportDeliveryMethod

  • 让我们考虑<代码> RePvices方法> /COD>。在视图模型中,它是一个

    字符串
    。在您的
    报表
    对象上,它是
    ReportDeliveryMethod
    类型。由于
    字符串
    不能隐式转换为
    ReportDeliveryMethod
    UpdateModel
    不会绑定它

    那么你的选择是什么

  • 手动绘制地图,就像您现在所做的那样
  • 绑定ID,而不是对象引用。EF4支持。您可以在视图模型中放置
    ReportDeliveryMethodId
    ,而不是
    ReportDeliveryMethod

  • 非常感谢Craig,这已经解决了我关于创建的问题,您的一些其他答案将对更新有用!非常感谢Craig,这已经解决了我关于创建的问题,您的一些其他答案将对更新有用!
    <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PermitLookup.Models.ReportViewModel>" %>
    <% using (Html.BeginForm())
       {%>
    <%: Html.ValidationSummary(true) %>
    <fieldset>
        <legend>Fields</legend>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.ShareName) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Report.ShareName) %>
            <%: Html.ValidationMessageFor(model => model.Report.ShareName)%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.Description) %>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Report.Description)%>
            <%: Html.ValidationMessageFor(model => model.Report.Description)%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.Frequency)%>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Report.Frequency)%>
            <%: Html.ValidationMessageFor(model => model.Report.Frequency)%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.SendTime)%>
        </div>
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Report.SendTime)%>
            <%: Html.ValidationMessageFor(model => model.Report.SendTime)%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.ReportDeliveryMethod)%>
        </div>
        <div class="editor-field">
            <%=Html.DropDownListFor(model => model.ReportDeliveryMethod, Model.ReportDeliveryMethods)%>
            <%: Html.ValidationMessageFor(model => model.Report.ReportDeliveryMethod)%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.ReportReceiver)%>
        </div>
        <div class="editor-field">
            <%=Html.DropDownListFor(model => model.ReportReceiver, Model.ReportReceivers)%>
            <%: Html.ValidationMessageFor(model => model.Report.ReportReceiver)%>
        </div>
        <div class="editor-label">
            <%: Html.LabelFor(model => model.Report.PermitType)%>
        </div>
        <div class="editor-field">
            <%=Html.DropDownListFor(model => model.PermitType, Model.PermitTypes)%>
            <%: Html.ValidationMessageFor(model => model.Report.PermitType)%>
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
    <% } %>