How to use different models in one View and pass the value to controller

对着背影说爱祢 提交于 2020-06-29 05:03:11

问题


I have one View from where I need to pass the Id_cliente to controller.

This is my Model:

 public class Cliente
    {
        [Key]
        public int Id { get; set; }
        [Display(Name = "Azienda")]
        public string Nome_azienda { get; set; }
    }

 public class SottoCliente
    {
        [Key]
        public int Id { get; set; }
        public int Id_cliente { get; set; }
    }

In my View by clicking on the button Sotto Clienti I need to pass the Id_cliente from Model SottoCliente to controller. I don't know how to access the Id_cliente. This is my View :

@model IEnumerable<GestioneAtivita.Models.Cliente>

<table class="table">
    <thead>
        <tr>
            <th>
               ...
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Nome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cognome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Nome_azienda)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cellulare)
            </td>
            <td>
                //Here I have button, and here I need somthing like  new { clienteId = item.`Id_cliente`} to pass Id_cliente to controller
                @Html.ActionLink("Sotto Clienti", "CaricaSottoCliente", new { clienteId = item.Id}, new { @class = "btn btn-danger" })
            </td>
            <td> 
                @Html.ActionLink("Modifica", "ModificaCliente", new { id = item.Id }, new { @class = "btn btn-primary" }) |
                @Html.ActionLink("Elimina", "EliminaCliente", new { id = item.Id }, new { @class = "btn btn-danger" })
                </td>
            </tr>
        }
    </tbody>
</table>

And CaricaSottoCliente is the action where I'm trying to load the records from database based on Id_cliente :

public ActionResult CaricaSottoCliente(int clienteId)
    {
        if (clienteId == null)
        {
            return RedirectToAction("Index");
        }

        SottoCliente sottoCliente = _db.tboSottoClienti
            .Include(a => a.Nome)
            .Where(a => a.Id == clienteId)
            .SingleOrDefault();

        if (sottoCliente == null)
        {
            return  RedirectToAction("Index");
        }

        var view = new ViewModels
        {
            Id = sottoCliente.Id,
            Nome = sottoCliente.Nome,
            //ListaSottoClientis = sottoCliente.getCliente.ToList()
        };
        return View(view);

Everything works fine, but I don't get Id_cliente , instead I get the Id from Client Model.

Any ideas how to pass the Id_cliente to the controller?

Thanks in advance!


回答1:


You can create another class like this.

public class Parent
{
    public Cliente Cliente { get; set; }
    public SottoCliente SottoCliente { get; set; }
}

public class Cliente
{
    [Key]
    public int Id { get; set; }
    [Display(Name = "Azienda")]
    public string Nome_azienda { get; set; }
}

public class SottoCliente
{
    [Key]
    public int Id { get; set; }
    public int Id_cliente { get; set; }
}

And in model declare like this @model IEnumerable<GestioneAtivita.Models.Parent> and use the properties accordingly.




回答2:


You need to create a ViewModel that contains both the classes SottoCliente and Cliente.

public class ClientViewModel
{
    public Cliente Cliente { get; set; }
    public SottoCliente SottoCliente { get; set; }
}

public class Cliente
{
    [Key]
    public int Id { get; set; }
    [Display(Name = "Azienda")]
    public string Nome_azienda { get; set; }
}

public class SottoCliente
{
    [Key]
    public int Id { get; set; }
    public int Id_cliente { get; set; }
}

In the View access the fields like this

@model IEnumerable<GestioneAtivita.Models.ClientViewModel>

<table class="table">
    <thead>
        <tr>
            <th>
               ...
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Cliente.Nome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cliente.Cognome)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cliente.Nome_azienda)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Cliente.Cellulare)
            </td>
            <td>
                //Here I have button, and here I need somthing like  new { clienteId = item.`Id_cliente`} to pass Id_cliente to controller
                @Html.ActionLink("Sotto Clienti", "CaricaSottoCliente", new { clienteId = item.SottoCliente.Id}, new { @class = "btn btn-danger" })
            </td>
            <td> 
                @Html.ActionLink("Modifica", "ModificaCliente", new { id = item.Cliente.Id }, new { @class = "btn btn-primary" }) |
                @Html.ActionLink("Elimina", "EliminaCliente", new { id = item.Cliente.Id }, new { @class = "btn btn-danger" })
                </td>
            </tr>
        }
    </tbody>
</table>


来源:https://stackoverflow.com/questions/62486052/how-to-use-different-models-in-one-view-and-pass-the-value-to-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!