Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create MVC Custom Model Binder for model tuples

Need assistance in creating a MVC Custom Model Binder to post multiple model tuple to controller. Never worked with custom model binder. Have looked at other answers to this issue but, don't seem to come close in dealing with a tuple of models or provide desired solution. Any ideas are appreciated. - Thanks

View

@model Tuple<Contact, Communications, Addresses>
@using (Html.BeginForm()) {
  <div id="contact">
    <div class="editor-label">
         @Html.LabelFor(m => m.Item1.FirstName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Item1.FirstName)
    </div>
    <div class="editor-label">
        @Html.LabelFor(m => m.Item1.LastName)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Item1.LastName)
    </div>
    <div>
        <input type="submit" value="Create" />
    </div>
  </div>
  <div id="communication">
    <div class="editor-label">
        @Html.LabelFor(m => m.Item2.TelephoneValue)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Item2.TelephoneValue)
    </div>
  </div> 
  <div id="address">
    <div class="editor-label">
        @Html.LabelFor(m => m.Item3.Address1)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Item3.Address1
    </div>
    <div class="editor-label">
        @Html.LabelFor(m => m.Item3.City)
    </div>
    <div class="editor-field"> 
        @Html.TextBoxFor(m => m.Item3.City)
    </div>
    <div class="editor-label">
        @Html.LabelFor(m => m.Item3.StateProvince)
    </div>
    <div class="editor-field">
        @Html.TextBoxFor(m => m.Item3.StateProvince)
    </div>
    <div class="editor-label">
        @Html.LabelFor(m => m.Item3.PostalCode) 
    </div>
    <div class="editor-field"> 
        @Html.TextBoxFor(m => m.Item3.PostalCode, new { id = "zip", style = "width:90px;" })
    </div>
  </div> 
}

Contoller

[HttpPost]
public ActionResult CreateContact(Tuple<Contact, Communications, Addresses> tuple) {
      //…. Do tuple processing to transfer values to add values to App Service here.
}
like image 360
FrankDev Avatar asked Jun 01 '26 08:06

FrankDev


1 Answers

Why dont you try keeping the "Contact, Communications, Addresses" models inside a new model and bind it to the view.

It will make handling very simple.

like image 154
Moin Avatar answered Jun 04 '26 11:06

Moin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!