0

モデルからドロップダウンリストを作成する MVC3 アプリケーションがあります。('/Edit/4')リストからアイテムを選択するとき、モデル内のすべてのレコードの編集リンクを作成するテンプレートを使用するのではなく、編集ビューを表示できる単一の「編集」リンクでURL を更新したいと思います 。 1 つの編集リンクを使用し、ドロップダウン リストで項目が選択されたときにそれを更新したいと考えています。私は jquery を使用してこれのいくつかを達成することができました。MVC を使用して C# コードで実行したいと思います。

何かご意見は??

4

1 に答える 1

3

サンプルコードを作成しました。エリアがあります。強調表示された部分は、関係するコードです。

ここに画像の説明を入力

コントローラ

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();
    }
}

モデル

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 ビュー

@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>

強調表示された領域の下の MyPractiseAreaRegistration クラスの下の領域登録

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 }
        );
    }
}
于 2013-06-16T07:09:32.100 に答える