如何将数据从表单传递到我的控制器,然后回到我的客户端javascript?

时间:2017-02-28 15:46:19

标签: javascript asp.net asp.net-mvc

我的网页上有一个表单,要求输入名字和姓氏。当用户提交此表单时,我希望将数据传递给我的控制器中的函数,在该函数中,它用于在我们的Active Directory中查找相应的用户。我想将匹配的电子邮件地址返回到Javascript函数,然后在我的页面上显示该信息。这是"正确"这样做的方法,如果是的话,我如何在我的控制器中构建一个函数来接受表单输入,并将数据返回给我的客户端javascript?

到目前为止,我的控制器中有什么:

 public SearchResult[] searchAD(String fname, String lname)
    {
        Func<System.DirectoryServices.ActiveDirectory.Domain> domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain;
        System.DirectoryServices.DirectoryEntry root = new DirectoryEntry("LDAP://"+domain);
        DirectorySearcher searcher = new DirectorySearcher();
        searcher.SearchRoot = root;
        searcher.SearchScope = SearchScope.Subtree;
        searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0})(sn={1}))", fname, lname);
        SearchResult[] results = new SearchResult['a'];
        searcher.FindAll().CopyTo(results, 0);
        return results;
    }

我的表格:

@using (Html.BeginForm("searchAD", "AD", FormMethod.Post))
    {
        @Html.AntiForgeryToken()

        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        <div class="form-group">
            @Html.LabelFor(m => m.firstName, "First Name", htmlAttributes: new { @class = "control-label" })
            <div>
                @Html.EditorFor(model => model.firstName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.firstName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(m => m.lastName, "Last Name", htmlAttributes: new { @class = "control-label" })
            <div>
                @Html.EditorFor(model => model.lastName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.lastName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div>
                <input type="submit" value="Search" class="btn" />
            </div>
        </div>
    }

2 个答案:

答案 0 :(得分:2)

您可以在TempData中存储值并在视图中获取

<Window x:Class="Core.View.Windows.MainWindow" ...>
    <Window.DataContext>
        ...
    </Window.DataContext>
    <Window.Resources>
        ...
    </Window.Resources>
    <Grid>

        <ListBox ...>
            ...
        </ListBox>

       <UserControl1 Visibility="{SomeBindingWithConverter}" />
       <UserControl2 Visibility="{SomeBindingWithConverter}" />
       <UserControl3 Visibility="{SomeBindingWithConverter}" />
       <UserControl4 Visibility="{SomeBindingWithConverter}" />
       <UserControl5 Visibility="{SomeBindingWithConverter}" />
    </Grid>
</Window>

在视图中(Jquery)

 public ActionResult searchAD(String fname, String lname)
    {
        Func<System.DirectoryServices.ActiveDirectory.Domain> domain = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain;
        System.DirectoryServices.DirectoryEntry root = new DirectoryEntry("LDAP://"+domain);
        DirectorySearcher searcher = new DirectorySearcher();
        searcher.SearchRoot = root;
        searcher.SearchScope = SearchScope.Subtree;
        searcher.Filter = string.Format("(&(objectCategory=person)(objectClass=user)(givenName={0})(sn={1}))", fname, lname);
        SearchResult[] results = new SearchResult['a'];
        searcher.FindAll().CopyTo(results, 0);
        TempData["Result"]=results;
        return View("ViewName");
    }

答案 1 :(得分:2)

以上不是您发布的问题的完美解决方案,因为tempdata或viewbag不适用于此。如果您需要再次从控件发布数据再查看,那么它总是使用@ Ajax.BeginForm

   @using (Ajax.BeginForm("ActionName", "Controller", new AjaxOptions {  OnComplete = "Sucess" }))

在jquery中你必须编写如下的Success函数:

function Sucess(arg) {
   //arg.data
}

在arg.data中你将得到你从控制器传递的对象。更多@Html.BeginForm刷新你的整个页面而不是特定内容但是使用Ajax.begin表单你可以设法重新加载那些在ajax.begin表格。