Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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 ?
Posted
Updated 13-May-14 21:42pm
v2
Comments
deepakaitr12345 14-May-14 10:34am    
HI @virang_21

I have 2 suggestions for you
1)If you restrict the selection on property type then use java script or jquery and ask user to select the value from the drop down

2)Another point here is if you want to post then check that(ModelState IsValid) and place the check on your model property(means place [Required] data-annotation attribute with valid initial value)

after that check in the model and its value if it is not valid then use modelstate.adderrorr(model.property)------(here your validation summary will work)

and your problem gets resolved

Hope this will help you..

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900