将具有不同SelectedValues的相同SelectList传递给View?

时间:2017-12-07 10:59:18

标签: asp.net-mvc-4 viewbag selectlist

我有一个有两个下拉菜单的视图,周末第1天和周末第2天。我处于编辑模式,从行动中我正在通过我的选择列表:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication16
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Book.books = doc.Descendants("books").FirstOrDefault().Elements().Select(x => new Book() {
                name = x.Name.LocalName,
                author = (string)x.Element("author"),
                detail = (string)x.Element("details")
            }).ToList();
        }
    }

    public class Book
    {
        public static List<Book> books = new List<Book>();

        public string name { get; set;}
        public string author { get; set;}
        public string detail { get; set;}
    }



}

我创建两次相同的列表只是为了传递不同的选定值。有没有办法只创建一次并使用不同的选定值传递它。我不想为这个创建新的数据库表。

更新:

 class ContainerCell: UICollectionViewCell {
     var widget: SmallWidget!

     func refresh(_ completion: @escaping () -> () = {}) {
        ....

         WidgetsService().runOneDashboard(widgetDashboardId: widget.model.widgetDashboardId) {[unowned self] result in
         if self.widget != nil {
            DispatchQueue.main.async {
                self.widget.fillForms(dictionary: result)
                widget.activityIndicator.stop()
                completion()
            }
        }
    }

      private func addWidgetToView(_ widget: SmallWidget) {
          customContentView.addSubview(widget)
          widget.translatesAutoresizingMaskIntoConstraints = false
          NSLayoutConstraint.activate([
              customContentView.topAnchor.constraint(equalTo: widget.topAnchor),
              customContentView.bottomAnchor.constraint(equalTo: widget.bottomAnchor),
              customContentView.leftAnchor.constraint(equalTo: widget.leftAnchor),
              customContentView.rightAnchor.constraint(equalTo: widget.rightAnchor)
        ])
      }
}


class SmallWidget: UIView { ... }

class ClientsTopWidgetView: SmallWidget {
    .....
    override func updateWidget() {
        super.updateWidget()
        models = []
        service.runOneDashboard(widgetDashboardId: model.widgetDashboardId, startDate: model.startDate, endDate: model.endDate) { result in
            self.dropDownPeriod.model = self.model
           self.getSortedClients(result)
           DispatchQueue.main.async {
               self.tableView.reloadData()
               self.activityIndicator.stop()
        }
    }
    ....
}

1 个答案:

答案 0 :(得分:1)

型号:

public class CustomModel
{
     public string Week1 { get; set; }
     public string Week2 { get; set; }
     public SelectList Items { get; set; }
}

控制器:

public ActionResult Index()
{
     CustomModel model = new CustomModel();         
     model.Items = new SelectList(new List<SelectListItem>
                            {
                                 new SelectListItem { Value = "", Text = "Select Day" },
                                 new SelectListItem { Value = "1", Text = "Sunday" },
                                 new SelectListItem { Value = "2", Text = "Monday" },
                                 new SelectListItem { Value = "3", Text = "Tuesday" },
                                 new SelectListItem { Value = "4", Text = "Wednesday" },
                                 new SelectListItem { Value = "5", Text = "Thursday" },
                                 new SelectListItem { Value = "6", Text = "Friday" },
                                 new SelectListItem { Value = "7", Text = "Saturday" }
                             }, "Value", "Text");
     return View(model);
}

查看:

@using (Html.BeginForm())
{
     @Html.DropDownListFor(model => model.Week1, Model.Items)
     @Html.DropDownListFor(model => model.Week2, Model.Items)
     <input type="submit" value="SUBMIT" />
}

对于post方法中的这种方法,您可以根据模型属性获取值。