0

我试图只使用 1 个 html.editorfor 在我的每个模型中填写两个模型字段。

我希望这个 editorfor 的值也被插入到我的 Clientes0013.Client0013

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

这个也被插入到 Clientes0013.F1Pais00200013

<div class="editor-field">
                @Html.EditorFor(model => model.CanaClie0012.F1Pais00200012)
                @Html.ValidationMessageFor(model => model.CanaClie0012.F1Pais00200012)
            </div>

请告诉我什么应该是正确的方法。

我的表模型:

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 CanaClie0012 CanaClie0012 { get; set; }
  public Clientes0013 Clientes0013 { get; set; }
}

我的控制器:

[HttpPost]
        public ActionResult ClientCreate(CanaClie0012 canaclie0012, Clientes0013 clientes0013)
        {
            ClientModel vm = new ClientModel();
            if (ModelState.IsValid)
            {
                db.CanaClie0012.Add(canaclie0012);
                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.CanaClie0012.Client00130012)
            @Html.ValidationMessageFor(model => model.CanaClie0012.Client00130012)
        </div>
        <div class="editor-label">
            Pais
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CanaClie0012.F1Pais00200012)
            @Html.ValidationMessageFor(model => model.CanaClie0012.F1Pais00200012)
        </div>
        <div class="editor-label">
            Address 
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CanaClie0012.Direcc0012)
            @Html.ValidationMessageFor(model => model.CanaClie0012.Direcc0012)
        </div>
         <div class="editor-label">
            Contact Number 
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Clientes0013.Nombre0013)
            @Html.ValidationMessageFor(model => model.Clientes0013.Nombre0013)
        </div>


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

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

1 回答 1

0

明白了.. 将我的控制器操作修改为这个。

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

            return View(vm);
        }
于 2013-06-11T04:54:48.483 回答