简体   繁体   English

视图渲染后将数据从javascript发送到控制器

[英]Sending data from javascript to controller after view has rendered

I currently have a controller that has a GET parameter. 我目前有一个具有GET参数的控制器。 My Javascript grabs the GET parameter and uses it to grab data from an API. 我的Javascript抓取GET参数,并使用它从API抓取数据。 The issue im having is im not sure how to set up my controller side functions/actionresults to accept ajax data and modify the viewmodel. 我所遇到的问题是我不确定如何设置我的控制器端函数/ actionresults以接受ajax数据并修改视图模型。 Do i need a seperate function or just modify the HTML from the client-side. 我需要单独的功能还是仅从客户端修改HTML。

Controller: 控制器:

public ActionResult Search(string locationinput)
        { 
             //ViewModel data processing...
             return View();
        }

Search View: 搜索视图:

         @model IEnumerable<Project.ViewModels.SearchResultsViewModel>

         <div class="container">
                    @foreach (var item in Model)
                    {
                            @Html.DisplayFor(modelItem => item.formatted_address)     
                            @Html.DisplayFor(modelItem => item.zip)
                    }
         </div>
public PartialViewResult Search(string locationinput)
        {
            List<SearchResultsViewModel> srvm=getSomething(locationinput);

            return PartialView(srvm);
        }

Search view 搜索视图

@model IEnumerable<Project.ViewModels.SearchResultsViewModel>

         <div class="container">
                    @foreach (var item in Model)
                    {
                            @Html.DisplayFor(modelItem => item.formatted_address)     
                            @Html.DisplayFor(modelItem => item.zip)
                    }
         </div>

and js 和js

$('#btnAjax').click(function () {
        $.ajax({
            url: '/Controller/Search',
            type: 'GET',
            dataType: 'html'

        })
        .success(function (result) {
            $('#searchresults').html(result);
        })
        .error(function (xhr, status) {
            alert(status);
        })
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM