如何从下拉列表中获取值并分配给MVC中的对象

时间:2017-06-15 12:57:33

标签: c# asp.net-mvc

对MVC来说很新。我正在为我的项目开发一个小工具。我想从下拉列表中获取所选值并分配给对象以进一步使用。我的代码看起来像这样。

查看:

    @model SolutionMasterDataRepl.Models.ReplDataModel

@{
  ViewBag.Title = "Solution Master";
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script>
        $(document).ready(function () {
            $('.btn-toggle').click(function () {
                $(this).find('.btn').toggleClass('active');
                if ($(this).find('.btn-primary').size() > 0) {
                    $(this).find('.btn').toggleClass('btn-primary');
                }
                if ($(this).find('.btn-danger').size() > 0) {
                    $(this).find('.btn').toggleClass('btn-danger');
                }
                if ($(this).find('.btn-success').size() > 0) {
                    $(this).find('.btn').toggleClass('btn-success');
                }
                if ($(this).find('.btn-info').size() > 0) {
                    $(this).find('.btn').toggleClass('btn-info');
                }
                $(this).find('.btn').toggleClass('btn-default');
            });
        });
    </script>
    <style>
        .form-horizontal{
            margin: 0 auto;
            width: 50%
        }
    </style>
</head>
<body>
    <div class="form-horizontal">
        <fieldset>
            <legend>Data REPL</legend>
            <div class="form-group">
                <label for="selectReplServer" class="col-lg-4">Select REPL Server</label>
                <div class="col-lg-offset-4">
                    <select class="form-control" id="replServer" style="width: 200px">
                        <option>PTW01REPLAP001</option>                                            
                    </select>
                    @Html.DropDownListFor(m => m.SelectedReplDatabase, Model.ReplDatabase, "Select REPL Server")
                </div>                
            </div>

模特:

    public class ReplDataModel
  {
    public SelectList ReplDatabase { get; set; }
    public string SelectedReplDatabase { get; set; }
  }

CONTROLLER:

public class HomeController : Controller
  {
    string replDatabase = string.Empty;
    // GET: Home
    public ActionResult Index()
    {
      return View();
    }

    [HttpPost]
    public ActionResult GetReplDatabase(ReplDataModel model)
    {
      replDatabase = model.SelectedReplDatabase;
      return View(model);
    }
  }

这里得到空引用异常: @Html.DropDownListFor(m => m.SelectedReplDatabase, Model.ReplDatabase, "Select REPL Server") 你能帮助我解决错误并建议更好的方法从下拉列表中分配值。

1 个答案:

答案 0 :(得分:0)

public class HomeController : Controller
  {
    string replDatabase = string.Empty;
    // GET: Home
    public ActionResult Index()
    {
      ReplDataModel Model = new ReplDataModel();
      return View(Model);
    }

    [HttpPost]
    public ActionResult GetReplDatabase(ReplDataModel model)
    {
      replDatabase = model.SelectedReplDatabase;
      return View(model);
    }
  }

您没有在Get方法中传递Model值,也在post方法中检查replDatabase。你没有分配任何东西。