mvc pass tuple data as parameter

时光怂恿深爱的人放手 提交于 2020-03-05 06:58:38

问题


I have a tuble like this as model.

@model   Tuple<Urun,List<UrunKatagori>>

inside the view I need to pass those data to controler.

here is the my button.

 Html.X().Button().Text("Guncelle").Icon(Icon.PageSave)
       .DirectEvents(de =>
       {
           de.Click.Url = "Urunler/Guncelle";
        de.Click.ExtraParams.Add(new Parameter { Name = "Urun", Value ="Model.Item1", Mode = ParameterMode.Raw });//Iguess here is wrong
       })

and my controller

[HttpPost]
public  ActionResult Guncelle (Urun  Urun){

    Urun_BLL urun_bll = new Urun_BLL();

  //  urun_bll.Update(mdl);
    X.Msg.Notify(new NotificationConfig
    {
        Icon = Icon.Accept,
        Title = "Working",
        Html =Urun.Ad.ToString()//I need to get data here
    }).Show();


    return this.Direct();
}

回答1:


I strongly suggest that you create a viewmodel class, rather then passing a Tuple e.g.

public class GuncelleViewModel
{
    public Urun Urun { get ;set; }
    public List<UrunKatagori>> UrunKatagori { get; set; }
}

Then you can pass that to the view like so:

[HttpPost]
public  ActionResult Guncelle (Urun  Urun)
{
    Urun_BLL urun_bll = new Urun_BLL();

  //  urun_bll.Update(mdl);
    X.Msg.Notify(new NotificationConfig
    {
        Icon = Icon.Accept,
        Title = "Working",
        Html =Urun.Ad.ToString()//I need to get data here
    }).Show();

    var viewModel = new GuncelleViewModel()
    viewModel.Urun = Urun;
    viewModel.UrunKatagori = // TODO - However you get the categories.

    return View(viewModel);
    // this.Direct(); What does this.Direct() do? Replace it with calling the view instead, much        cleaner.
}

In the view, use the following model

@model GuncelleViewModel

Useing a viewmodel class, which is associated one-to-one with a view file (*.cshtml), is a very good practise. It can help keep your design clean and more flexible, rather then passing specific data types, such as Tuple.



来源:https://stackoverflow.com/questions/24575192/mvc-pass-tuple-data-as-parameter

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