【问题标题】:Scaffold Create and Edit Views with Dropdownlist in MVC脚手架在 MVC 中使用下拉列表创建和编辑视图
【发布时间】:2013-06-10 11:28:08
【问题描述】:

我有一个从模型填充下拉列表的 MVC3 应用程序。当我从列表中选择一个项目时,我想在单个“编辑”链接上更新 url ('/Edit/4'),这将允许我显示编辑视图,即,而不是使用为所有记录创建编辑链接的模板模型,我想使用一个编辑链接,然后在下拉列表中选择项目时对其进行更新。我已经能够使用 jquery 实现其中的一些,我想使用 MVC 在 C# 代码中完成它。

有什么想法吗??

【问题讨论】:

  • DropDownlist 或 selectlistbox ?
  • 我创建了一个SelectList的实例,然后在DropDownList中使用了它
  • 你的意思是你有一个下拉列表和一个链接。在 ddl 中选择项目,然后单击链接将导航用户编辑页面,该页面的 url 以 /Edit/4 结尾
  • 没错!这就是我想要实现的目标

标签: asp.net-mvc asp.net-mvc-3 scaffolding


【解决方案1】:

我创建了一个示例代码。我有一个区域。突出显示的部分是您关注的代码。

控制器

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

【讨论】:

    猜你喜欢
    • 2011-03-22
    • 2011-08-07
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多