在视图中的多表

时间:2013-06-10 17:39:03

标签: asp.net-mvc model

我正在尝试创建一个强类型"Create View",这样我就可以同时将数据插入两个表中。但是,当我创建视图时,我的表中的字段不包含在我的模型中。你能说出这样做的正确方法吗?

我的表格模型:

public partial class CanaClie0012
    {
        public string Client00130012 { get; set; }
        public string F1Pais00200012 { get; set; }
        public string F1Cana02530012 { get; set; }
        public string Direcc0012 { get; set; }
        public Nullable<System.DateTime> TmStmp0012 { get; set; }
    }

public partial class Clientes0013
    {
        public string Client0013 { get; set; }
        public string Nombre0013 { get; set; }
        public string F1Pais00200013 { get; set; }
    }

我的自定义模型将两个表合并为:

public class ClientModel
{
    public IQueryable<CanaClie0012> CanaClie0012 { get; set; }
    public IQueryable<Clientes0013> Clientes0013 { get; set; }
}

我的控制器:(不确定我是否正确)

[HttpPost]
public ActionResult ClientCreate(CanaClie0012 canaclie0012, Clientes0013 clientes0013)
{
    ClientModel vm = new ClientModel();
    if (ModelState.IsValid)
    {
        db.CanaClie0012.Add(canaclie0012);
        db.SaveChanges();
        db.Clientes0013.Add(clientes0013);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    return View(vm);
}

我的观点:

@model MvcApplication1.Models.ClientModel

@{
    ViewBag.Title = "ClientCreate";
}

<h2>ClientCreate</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>ClientModel</legend>

        <div class="editor-label">
            Client Name
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Client00130012)
            @Html.ValidationMessageFor(model => model.Client00130012)
        </div>
        <div class="editor-label">
            Pais
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.F1Pais00200012)
            @Html.ValidationMessageFor(model => model.F1Pais00200012)
        </div>
        <div class="editor-label">
            Address 
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Direcc0012)
            @Html.ValidationMessageFor(model => model.Direcc0012)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Nombre0013)
            @Html.ValidationMessageFor(model => model.Nombre0013)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

1 个答案:

答案 0 :(得分:1)

一个大问题是,您的ClientModel课程的视图类型很强。此类不包括您在视图中引用的成员。相反,该类包括包含这些成员的对象列表。此时,您的视图可能甚至无法编译。

尝试更改ClientModel课程,如下所示:

public class ClientModel
{
  public CanaClie0012 CanaClie0012 { get; set; }
  public Clientes0013 Clientes0013 { get; set; }
}

然后您的视图将引用成员,例如:

<div class="editor-label">
  Client Name
</div>
<div class="editor-field">
  @Html.EditorFor(model => model.CanaClie0012.Client00130012)
  @Html.ValidationMessageFor(model => model.CanaClie0012.Client00130012)
</div>

同样适用于Clientes0013成员的成员。

另外一件事:您无需在控制器中拨打db.SaveChanges();两次。相反,插入两个表并执行一次保存:

db.CanaClie0012.Add(canaclie0012);
db.Clientes0013.Add(clientes0013);
db.SaveChanges();