简体   繁体   English

使用一个html.editorfor填写两个模型字段

[英]Using one html.editorfor to fill in two model fields

Im trying to use only 1 html.editorfor to fill in two model field in each of my model. 我试图只使用1 html.editorfor来在我的每个模型中填写两个模型字段。

I want the value of this editorfor to be also inserted to my Clientes0013.Client0013 我希望将此editorfor的值也插入到我的Clientes0013.Client0013中

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

and this one to be also inserted to Clientes0013.F1Pais00200013 并将其也插入到Clientes0013.F1Pais00200013中

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

Kindly show me what should be the right way of doing this. 请告诉我什么是正确的方法。

My Table Model: 我的表格模型:

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

My Custom Model to combine the two table is: 结合这两个表的我的自定义模型是:

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

My Controller: 我的控制器:

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

My View: 我的观点:

@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>

Got it.. modified my controller action to this one. 明白了..修改了我的控制器动作。

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

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

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