MVC3 Drop Down List Control does not keep State, Why?

前端 未结 2 1331
臣服心动
臣服心动 2021-01-24 01:56

I have a drop down list control on a partial view and after posting the form the state of the drop down list control is not maintained

Here is the partial view cshtml co

相关标签:
2条回答
  • 2021-01-24 02:49

    The following code creates the view model for your next partial view:

    TestCriteriaConsolidated combined = new TestCriteriaConsolidated();
    combined.testCriteriaResults = test;
    

    You need to set the year on this model:

    combined.testCriteria.Year = Request.Form["testCriteria.Year"];
    
    0 讨论(0)
  • 2021-01-24 02:52

    combined.testCriteria.Year needs to get set with the form value you posted

    Try this

    [HttpPost]
        public ActionResult Results(TestCriteriaConsolidated form)
        {
            List<TestCriteriaResults> test = new List<TestCriteriaResults>();
            test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "one", Value3 = "three", Value4 = "Four" });
            test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
            test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
            test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
            test.Add(new TestCriteriaResults { Value1 = "one", Value2 = "two", Value3 = "three", Value4 = "four" });
    
            TestCriteria criteria = new TestCriteria() { Year = form.testCriteria.Year };
    
            TestCriteriaConsolidated combined = new TestCriteriaConsolidated
                                                    {
                                                        testCriteriaResults = test,
                                                        testCriteria = criteria
                                                    };
            return View(combined);
        }
    
        public ActionResult Results() 
        { 
            return View(new TestCriteriaConsolidated());
        }
    
    0 讨论(0)
提交回复
热议问题