简体   繁体   English

使用Ajax MVC4更新div

[英]update div with Ajax MVC4

I can't seem to understand how to update a div of items after a category is selected. 在选择类别后,我似乎无法理解如何更新项目的div。 I've got this far and am now confused. 我已经走了这么远,现在很困惑。 How to get the values from the view into the controller to make the query? 如何从视图中获取值到控制器以进行查询?

 //get <li> clicked and display the items in that category
 $(document).ready(function() {
$('#test li').click(function() {
    var selector = "input[name='" + $(this).id + "value']";
    var catID = $(selector).val(); 

    $.ajax({
        url: "...",
        type: "get",
        data: {//return all the item info},
        success: function(data) {
            //update div contents here?

        }
    });
});
});

Partial to be updated upon which category is clicked 单击部分将更新的部分

@foreach (var item in Model)
 {

    <ul>

        <li>@item.item_name</li>
        <li><input type="hidden" class="item_id" value="@item.ID" /></li>

    </ul>

 }

Controller 控制者

public ActionResult PartialTwo( int id)//how to pass category id?
    {

        var query = from d in db.Items
                    where d.catId==id
                    orderby d.dateAdded
                    select d;
        var results = query;


        return PartialView("_FullInfoPartial", results);
        //returns items in the clicked category  

    }

instead of two li 而不是两个

<li>@item.item_name</li>
<li><input type="hidden" class="item_id" value="@item.ID" /></li>

you can just use one in order to decrease dom size if it is not required 您可以只使用一个来减小dom的大小(如果不需要)

<li data-id="@item.ID">@item.item_name</li>

and you can get the partial view result with $.get easily 您可以使用$ .get轻松获得部分视图结果

$("#test li").on("click",function(){

    var requestPage = "/PartialTwo?id=" + $(this).data("id");
    $.get(requestPage, function(result){
        $("#content").html(result); // where you want to put the result
    });
});

I've answered a very similar question over here, though the example was using PHP. 我在这里回答了一个非常类似的问题,尽管示例使用的是PHP。 The basic idea is the same however. 基本思想是相同的。 your MVC4 will return some data type that will be turned into HTML (the easiest way is to just return straight HTML) and then you will append this HTML value to a DOM element (in this case, the div item of your choice). 您的MVC4将返回某种数据类型,该数据类型将转换为HTML(最简单的方法是直接返回纯HTML),然后将这个HTML值附加到DOM元素(在这种情况下,您选择的div项)。

the JQuery would look similar too: JQuery也看起来类似:

var request = $.ajax({
      type: "POST",
      url: "example.php",
      data: data_obj,
      dataType: "html"
   }).done(function(msg) {
         $("#example_element").append(msg);
      }

Attempting to load a "div" as dynamic content is returned 尝试在返回动态内容时加载“ div”

Use Ajax.BeginForm as follows, 如下使用Ajax.BeginForm

@using (Ajax.BeginForm("Index", "Home", new AjaxOptions { HttpMethod = "POST", UpdateTargetId = "youDivId" }))

Using the above and making your controller returning Patial View will update your div with the partial view result . 使用以上方法并使您的控制器返回Patial View将使用partial view result更新div。

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

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