1

I'm new in the ASP.NET Framework, I've read the fundamental and have some understanding(theory) on the framework but not much in practice.

I'm struggling with the dropdownlistfor helper method, it comes down to having a weird behavior when i attempt to change the value of the selected item programatically.

In my controller i have the Index action method that receives a parameter of type Tshirt, inside this action method i set the property named Color of the Tshirt object with a value of 2.

In the view (strongly typed) i have a list of colors and pass this list as an argument for the constructor of the ColorViewModel class that will be in charge of returning SelectListItems for my list of colors.

In the view I then set the Selected property of my ColorViewModel object with the value coming from model.Color, now i have set everything so that when i call

@Html.DropDownListFor(x => x.Color, cvm.SelectItems, "Select a color")

I will have my dropdown displayed with the selected item.

When the request(GET) is performed the page is rendered by the browser and the dropdownlist appears with the correct value selected that was established with the value 2 in the Index action method, the dropdown displays "Blue" this is correct.

Then if i select a different item in the dropdownlist (RED, having an id of one) and submit the form, the Save action method is called and i know that the model is reaching the action method with a model.Color=1, which is correct.

Then i decide to redirect to the index action method passing the model object, the index action method changes the Color property back to 2, so when the page is rendered it should display again the value of Blue in the dropdown, but it doesn't, it displays Red.

if you comment out the following line in the Save action method you will get a different behavior.

//tshirt.Color = 3;

i know this logic im following doesnt make much sense from a bussines logic perspective, im just trying to understand what i am doing wrong to not get the expected result. Given the following model

public class Color
{
    public int Id { get; set; }
    public string Description { get; set; }
}

I Create the following view model

public class ColorViewModel
{
    public int Selected { get; set; }
    private List<Color> Colors;
    public IEnumerable<SelectListItem> SelectItems { get { return new SelectList(this.Colors, "Id", "Description", this.Selected); } }
    private ColorViewModel() { }
    public ColorViewModel(List<Color> colors)
    {
        this.Colors = colors;
    }
}

This is my Controller

public class HomeController : Controller
{
    [HttpGet()]
    public ActionResult Index(Tshirt tshirt)
    {
        tshirt.Color = 2;
        Tshirt t = new Tshirt();
        t.Color = tshirt.Color;            
        return View(t);
    }
    [HttpPost()]
    public ActionResult Save(Tshirt tshirt)
    {
        //tshirt.Color = 3;
        return RedirectToAction("Index", tshirt);
        //return View("Index",tshirt);
    }
}

And Finally my View

    @{
        List<Color> colors = new List<Color>(){
            new Color(){Id=1, Description="Red"},
            new Color(){Id=2, Description="Blue"},
            new Color(){Id=3, Description="Green"}
        };
        ColorViewModel cvm = new ColorViewModel(colors) { Selected = Model.Color };
    }

    @using(@Html.BeginForm("Save","Home")){
        @Html.DropDownListFor(x => x.Color, cvm.SelectItems, "Select a color")
            <input type="submit" />
    }

I have uploaded the complete code: VS Solution

1 Answer 1

0

Because when you redirect, you are passing the updated model

return RedirectToAction("Index", tshirt);
Sign up to request clarification or add additional context in comments.

2 Comments

Yea the updated model is being passed but once inside the Index action method the value is changed to another and the dropdownlist doesn't show this programmatically selected item, it displays the one that the user clicked on.
Can you post your Tshirt class and other necessary code so we can try it out at our side

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.