验证在MVC中通过POST访问的视图 [英] Validating View that was accessed via POST in MVC

查看:76
本文介绍了验证在MVC中通过POST访问的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习MVC,我理解基本概念,但我坚持这个场景。我有一个View Called UnitType,我需要转到PropertyType视图。我对UnitType进行了验证,以确保用户在转到PropertyType之前选择单元类型。这很好用。我还对PropertyType进行了验证,以确保用户选择属性类型。如果用户没有选择我希望将用户发送回PropertyType视图的属性类型,但我在这一点上正在努力。我使用POST将用户从UnitType发送到PropertyType视图。



I am learning MVC and I understand the basic concepts but I am stuck with this scenario. I have a View Called UnitType from there I need to go to PropertyType view. I have a validation on UnitType to make sure user selects type of unit before going to PropertyType. This works well. I also have validation on PropertyType to make sure user select Type of Property. If user does not select type of property I wanted to send user back to PropertyType view but I am struggling at that point. I ma using POST to send user from UnitType to PropertyType view.

public ActionResult UnitType() // STEP 1 : DISPLAY UnitType
        {
            UnitTypeModel objModel = new UnitTypeModel();

            objModel.TypeOfUnit = new List<string>();
            objModel.TypeOfUnit.Add("Evaporative Cooler");
            objModel.TypeOfUnit.Add("Gas Ducted Heater");
            objModel.TypeOfUnit.Add("Add On Cooling");
            objModel.TypeOfUnit.Add("Ducted Reverse Cycle");

            return View(objModel);

        }

        [AcceptVerbs(HttpVerbs.Post)]
        [HttpPost]
        public ActionResult UnitType(BrivisAfterSalesSupport.Models.UnitTypeModel objModel) // STEP 2 : POST
        {
            if (ModelState.IsValid) // ALL OK
            {
                Session["SelectedUnitType"] = objModel.SelectedUnitType;

                PropertyTypeModel objModelProp = new PropertyTypeModel();

                objModelProp.lstTypeOfProperty = new List<TypeOfProperty>() {

                         new TypeOfProperty {PropertyTypeId=1, PropertyTypeDescription="Single Story"},
                         new TypeOfProperty {PropertyTypeId=2, PropertyTypeDescription="Double Story or Higher"},
                         new TypeOfProperty {PropertyTypeId=3, PropertyTypeDescription="Multilevel Apartment"}

            };
                return View("PropertyType", objModelProp); // STEP 3 : GO TO PropertyType VIEW
            }
            else
            {

                return UnitType();
            }
        }

        public ActionResult PropertyType(BrivisAfterSalesSupport.Models.PropertyTypeModel objModel) //STEP 4 : DISPLAY PropertyTypes
        {
            return View(objModel);
        }

        [HttpPost]
        public ActionResult PropertyType(string SelectedPropertyType) //STEP 5: POST from PropertyType
        {
            if (!string.IsNullOrEmpty(SelectedPropertyType))
            {
                return View("UnitLocation");
            }
            else
            {


//This is where I have issue 



// IF I DO THIS COMMENTED BIT THE VALIDATION SUMMARY DOES NOT WORK 
                // PropertyTypeModel objModelProp = new PropertyTypeModel();
                // objModelProp.lstTypeOfProperty = new List<TypeOfProperty>() {

                //         new TypeOfProperty {PropertyTypeId=1, PropertyTypeDescription="Single Story"},
                //         new TypeOfProperty {PropertyTypeId=2, PropertyTypeDescription="Double Story or Higher"},
                //         new TypeOfProperty {PropertyTypeId=3, PropertyTypeDescription="Multilevel Apartment"}
                //};
                //return View(objModelProp);


                return View(); // THIS ONE ALSO FAIL AS lstTypeOfProperty is null which is used in VIEW to display type of properties to select from 
            }
        }





我正在做一些根本错误的事情。任何人都可以建议正确的方向吗?



I am doing something fundamentally wrong. Can anyone please suggest right direction ?

推荐答案

这篇关于验证在MVC中通过POST访问的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆