简体   繁体   中英

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.

I want the value of this editorfor to be also inserted to my 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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