ASP.NET MVC3: Interaction between Partial View and Main View

前端 未结 3 774
难免孤独
难免孤独 2020-12-20 09:55

I have a partial view for contact. Currently the index view shows this partial view for contact details. There is a save button inside the partial view to save the edited d

3条回答
  •  一个人的身影
    2020-12-20 10:25

    As data is posted to "HomeController" and "Index" action, so changes are reflected when you change age in View.

    Try to modify the home controller as follows,then it will work as expected.

    1) Instead of having a list of AgeHoroscope, we can have a dictionary of age and prediction.

    2) Create two Index Action for HttpGet and HttpPost as follows.

    public class HomeController : Controller
    {
    
        Dictionary AgePred = new Dictionary()
        {
        {16,"You are confused"},
        {26,"You are very brilliant"},
        {27,"You are practical"}
        };
    
        [HttpGet]
        public ActionResult Index()
        {
            AgeHoroscope selectedHoro = new AgeHoroscope() { Age = 26 };
            selectedHoro.HoroscopePrediction = AgePred[selectedHoro.Age];
            return View(selectedHoro);
        }
        [HttpPost]
        public ActionResult Index(AgeHoroscope model,ContactEntity entity)
        {
            model.Age = entity.ContactAge;
            model.HoroscopePrediction = AgePred[entity.ContactAge];
            return View(model);
        }
    
    }
    

提交回复
热议问题