Pass single Model from Tuple to Controller in ASP.Net Core MVC

AliYilmaz
 
on Jun 03, 2022 05:20 AM
Sample_236815.zip
1406 Views

Hi,

I need to draw two models in a view. Model 1 will register on the same page.

Model Article will record. Model 1 and 2 will be related to each other.

My goal is to save the database when I post the VMArtikel model.

How should we proceed in this case? Can you make an example? 

View

@model Tuple<VM_Beyanname, VM_Artikel>
 
 
<div class="content-page">
    <div class="content">
 
        <!-- Start Content-->
        <div class="container-fluid">
 
            <!-- start page title -->
            <div class="row">
                <div class="col-12">
                    <div class="page-title-box">
                        @*<div class="page-title-right">
                                <ol class="breadcrumb m-0">
                                    <li class="breadcrumb-item"><a href="javascript: void(0);">Abstack</a></li>
                                    <li class="breadcrumb-item"><a href="javascript: void(0);">Forms</a></li>
                                    <li class="breadcrumb-item active">Form Wizard</li>
                                </ol>
                            </div>*@
                        <h4 class="page-title">Beyanname Ekle</h4>
                    </div>
                </div>
            </div>
 
            <div class="row">
 
                <div class="col-md-2">
                    <div class="card-box">
 
                        <form asp-controller="Beyanname" asp-action="BeyannameAdd">
                            <section>
                                <div class="form-group clearfix">
                                    <label class="control-label">Beyanname NO *</label>
                                    <div class="">
                                        <input asp-for="Item1.BeyannameNO" class="form-control required" type="text">
                                    </div>
                                </div>
                                <div class="form-group clearfix">
                                    <label class="control-label"> Tarih *</label>
                                    <div class="">
                                        <input asp-for="Item1.Tarih" class="form-control" type="date">
                                    </div>
                                </div>
                            </section>
 
                            <div class="form-group clearfix text-right">
                                <button type="submit" class="text-right btn btn-primary">Kaydet</button>
                            </div>
                        </form>
                    </div>
                </div>
                <div class="col-md-10">
                    <div class="card-box">
                        <form id="basic-form" asp-controller="Beyanname" asp-action="ArtikelAdd">
                            <div>
                                <h3>Beyanname</h3>
                                <section>
                                    <div class="form-group clearfix">
                                        <label class="control-label">Beyanname NO *</label>
                                        <div class="">
                                            <input value="@ViewBag.Bey" class="form-control required" name="bNO" type="text">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="password"> Sezon Kodu *</label>
                                        <div class="">
                                            <input asp-for="Item2.SezonKodu" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="confirm">Artikel Kodu *</label>
                                        <div class="">
                                            <input asp-for="Item2.ArtikelKodu" type="text" class="required form-control">
                                        </div>
                                    </div>
                                </section>
                                <h3>Ürün</h3>
                                <section>
                                    <div class="form-group clearfix">
 
                                        <label class="control-label" for="name"> Kumas Kodu *</label>
                                        <div class="">
                                            <input asp-for="Item2.KumasKodu" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="surname">Icerik Bilgisi *</label>
                                        <div class="">
                                            <input asp-for="Item2.IcerikBilgisi" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="email">GTip *</label>
                                        <div class="">
                                            <input asp-for="Item2.Gtip" name="email" type="text" class="required email form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="address">Kalem Sayısı *</label>
                                        <div class="">
                                            <input asp-for="Item2.KalemSayisi" name="address" type="text" class="form-control">
                                        </div>
                                    </div>
                                </section>
                                <h3>Ağırlık</h3>
                                <section>
                                    <div class="form-group clearfix">
                                        <label class="control-label" for="name"> Adet *</label>
                                        <div class="">
                                            <input asp-for="Item2.Adet" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="surname">Birim Fiyat *</label>
                                        <div class="">
                                            <input asp-for="Item2.BirimFiyat" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="email">Brüt Kg *</label>
                                        <div class="">
                                            <input asp-for="Item2.BrutKg" type="text" class="required email form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="address">Net Kg *</label>
                                        <div class="">
                                            <input asp-for="Item2.NetKg" type="text" class="form-control">
                                        </div>
                                    </div>
                                </section>
                                <h3>Ürün Detay</h3>
                                <section>
                                    <div class="form-group clearfix">
 
                                        <label class="control-label" for="name"> Kap *</label>
                                        <div class="">
                                            <input asp-for="Item2.Kap" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="surname">Ticari Tanım *</label>
                                        <div class="">
                                            <input asp-for="Item2.TicariTanim" type="text" class="required form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="email">Cinsiyet Bilgisi *</label>
                                        <div class="">
                                            <input asp-for="Item2.CinsiyetBilgisi" type="text" class="required email form-control">
                                        </div>
                                    </div>
                                    <div class="form-group clearfix">
                                        <label class="control-label " for="address">Mensei Bilgisi *</label>
                                        <div class="">
                                            <input asp-for="Item2.MenseiBilgisi" type="text" class="form-control">
                                        </div>
                                    </div>
                                </section>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>

Controller

public IActionResult Index()
{
   
    Beyanname beyanname = new Beyanname();       
    beyanname = _beyannameReadRepository.GetWhere(x => x.Tarih != null).OrderByDescending(x => x.Tarih).FirstOrDefault();
    ViewBag.Bey = beyanname.BeyannameNO;
 
    return View();
}      
 
[HttpPost]
public async Task<IActionResult> ArtikelAdd([Bind(Prefix = "Item2")] VM_Artikel vM_Artikel)
{
    Artikel artikel = new Artikel();        
 
    artikel = _mapper.Map<Artikel>(vM_Artikel);
    artikel.BeyannameNO = bNO;
    await _artikelWriteRepository.AddAsync(artikel);
    await _artikelWriteRepository.SaveAsync();
 
    TempData["Mesaj"] = "Artikel Kaydedildi";
 
    return View();
}

CustomerModel 

public class CustomerModel
{
    public int CId { get; set; }
    public string CName { get; set; }
}

PersonModel  

public class PersonModel
{
    public int PId { get; set; }
    public string PName { get; set; }
    public int CId { get; set; }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Jun 04, 2022 02:51 AM
on Jun 11, 2022 02:56 AM

Hi AliYilmaz,

You need to use Bind Prefix the exclude binding to certain Items.

Since your values are prefixed with Item1 and Item2 in Tuple, you should indicate the Item to the model binder.

Check the below example.

Model

Customer

public class CustomerModel
{
    public int CId { get; set; }
    public string CName { get; set; }
}

Person

public class PersonModel
{
    public int PId { get; set; }
    public string PName { get; set; }
}

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public IActionResult AddCustomer([Bind(Prefix = "Item1")]CustomerModel customer)
    {
        TempData["Message"] = "Id: " + customer.CId + " Name: " + customer.CName;
        return RedirectToAction("Index");
    }

    [HttpPost]
    public IActionResult AddPerson([Bind(Prefix = "Item2")]PersonModel person)
    {
        TempData["Message"] = "Id: " + person.PId + " Name: " + person.PName;
        return RedirectToAction("Index");
    }
}

View

@using Tuple_Core_MVC.Models
@model Tuple<CustomerModel, PersonModel>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
</head>
<body class="container">
    <h1>Customer</h1>
    <form asp-controller="Home" asp-action="AddCustomer">
        <section>
            <div class="form-group clearfix">
                <label class="control-label">Id *</label>
                <div class="">
                    <input asp-for="Item1.CId" class="form-control required" type="text">
                </div>
            </div>
            <div class="form-group clearfix">
                <label class="control-label"> Name *</label>
                <div class="">
                    <input asp-for="Item1.CName" class="form-control" type="text">
                </div>
            </div>
        </section>
        <div class="form-group clearfix text-right">
            <button type="submit" class="text-right btn btn-primary">Add Customer</button>
        </div>
    </form>
    <h1>Person</h1>
    <form asp-controller="Home" asp-action="AddPerson">
        <section>
            <div class="form-group clearfix">
                <label class="control-label">Id *</label>
                <div class="">
                    <input asp-for="Item2.PId" class="form-control required" type="text">
                </div>
            </div>
            <div class="form-group clearfix">
                <label class="control-label">Name *</label>
                <div class="">
                    <input asp-for="Item2.PName" class="form-control" type="text">
                </div>
            </div>
        </section>
        <div class="form-group clearfix text-right">
            <button type="submit" class="text-right btn btn-primary">Add Person</button>
        </div>
    </form>
    <hr />
    @TempData["Message"]
</body>
</html>

Screenshot

dharmendr
 
on Jun 06, 2022 05:02 AM

Hi AliYilmaz,

Check the below example.

Model

Customer

public class CustomerModel
{
    public int CId { get; set; }
    public string CName { get; set; }
}

Person

public class PersonModel
{
    public int PId { get; set; }
    public string PName { get; set; }
    public int CId { get; set; }
}

Controller

public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public IActionResult AddCustomer([Bind(Prefix = "Item1")]CustomerModel customer)
    {
        TempData["Message"] = "Id: " + customer.CId + " Name: " + customer.CName;
        return RedirectToAction("Index");
    }

    [HttpPost]
    public IActionResult AddPerson([Bind(Prefix = "Item2")]PersonModel person)
    {
        TempData["Message"] = "Id: " + person.PId + " Name: " + person.PName + " CID: " + person.CId;
        return RedirectToAction("Index");
    }
}

View

@using Tuple_Core_MVC.Models
@model Tuple<CustomerModel, PersonModel>
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" />
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('#txtCId').change(function () {
                $('#hfCId').val($(this).val());
            });
        });
    </script>
</head>
<body class="container">
    <h1>Customer</h1>
    <form asp-controller="Home" asp-action="AddCustomer">
        <section>
            <div class="form-group clearfix">
                <label class="control-label">Id *</label>
                <div class="">
                    <input asp-for="Item1.CId" class="form-control required" type="text" id="txtCId">
                </div>
            </div>
            <div class="form-group clearfix">
                <label class="control-label"> Name *</label>
                <div class="">
                    <input asp-for="Item1.CName" class="form-control" type="text">
                </div>
            </div>
        </section>
        <div class="form-group clearfix text-right">
            <button type="submit" class="text-right btn btn-primary">Add Customer</button>
        </div>
    </form>
    <h1>Person</h1>
    <form asp-controller="Home" asp-action="AddPerson">
        <section>
            <div class="form-group clearfix">
                <label class="control-label">Id *</label>
                <div class="">
                    <input asp-for="Item2.CId" class="form-control required" type="hidden" id="hfCId">
                    <input asp-for="Item2.PId" class="form-control required" type="text">
                </div>
            </div>
            <div class="form-group clearfix">
                <label class="control-label">Name *</label>
                <div class="">
                    <input asp-for="Item2.PName" class="form-control" type="text">
                </div>
            </div>
        </section>
        <div class="form-group clearfix text-right">
            <button type="submit" class="text-right btn btn-primary">Add Person</button>
        </div>
    </form>
    <hr />
    @TempData["Message"]
</body>
</html>