Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a MVC3 application that populates a dropdownlist from a Model. When I select an item from the list , i would like to Update the url ('/Edit/4') on a single 'edit' link which will allow me display the edit view, i.e rather than use a template which creates edit links for all records in the model, I would like to use one edit link and then update it as items are selected in the dropdownlist. I have been able to achieve some of this using jquery , I would like to do it in C# code using MVC.

Any thoughts??

share|improve this question
    
DropDownlist or selectlistbox ? –  wuhcwdc Jun 14 '13 at 2:35
    
I created an instance of SelectList and then used it in the DropDownList –  lacoder Jun 14 '13 at 10:09
    
You mean you have a dropdownlist and a link. on selecting the item in ddl, and clicking the link will navigate the user to edit page which will have url ends with /Edit/4 –  wuhcwdc Jun 15 '13 at 1:12
    
exactly ! That's what I would like to accomplish –  lacoder Jun 15 '13 at 22:48

1 Answer 1

I created a Sample code. I have an area. Highlighted part is your concerned code.

enter image description here

Controller

public class DropdownController : Controller
{
    [HttpGet]
    public ActionResult DropDownListFor()
    {
        Practise.Areas.MyPractise.Models.Dropdown d = new Models.Dropdown();
        return View(d);
    }

    public ActionResult Edit(string Val)
    {
        return View();
    }
}

Model

public class Dropdown
{
    [Required(ErrorMessage = "Please select state")]
    public string StateId { get; set; }
    public int MyProperty { get; set; }
    public List<SelectListItem> States
    {
        get
        {
            return new List<SelectListItem>() 
                { 
                    new SelectListItem
                    { 
                        Text = "Punjab", 
                        Value = "Pb", 
                        Selected = false
                    },
                    new SelectListItem
                    { 
                        Selected = false, 
                        Value = "HM", 
                        Text = "Himachal"
                    }
                };
        }
    }

}

DropDownListFor View

@model Practise.Areas.MyPractise.Models.Dropdown
<!DOCTYPE html>
<html>
<head>
    <title>DropDownListFor</title>
    <script src="/Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $('#StateId').change(function () {
            if($('#StateId option:selected').val() == "")
                $('.edit').attr('href', "#");
            else
                $('.edit').attr('href', 
                    "@Url.Action("Edit", "Dropdown", 
                       new RouteValueDictionary(new { area = "MyPractise" }))" 
                           + "/" + $('#StateId option:selected').text());
            });
        });
    </script>
</head>
<body>
        @using (Html.BeginForm("DropDownListFor", "DropDown", FormMethod.Post, 
                                 new { id = "DropDownList" }))
        {
            @Html.DropDownListFor(m => m.StateId, Model.States, "select");
            @Html.ValidationMessageFor(m => m.StateId);
            <a class="edit" href="#">Edit</a>
            <input type="submit" name="Submit" value="Submit" />
        }
</body>
</html>

Area registration under MyPractiseAreaRegistration class under highlighted area

public class MyPractiseAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "MyPractise";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "MyPractise_default1",
            "MyPractise/{controller}/{action}/{Val}",
            new { action = "Index", Val = UrlParameter.Optional }
        );

        context.MapRoute(
            "MyPractise_default",
            "MyPractise/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional }
        );
    }
}
share|improve this answer
    
@lacoder : Did you find this answer useful ? –  wuhcwdc Jun 17 '13 at 17:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.