【问题标题】:How to update the Model value and reload a div in Razor view in MVC如何在 MVC 的 Razor 视图中更新模型值并重新加载 div
【发布时间】:2018-05-31 18:53:50
【问题描述】:

这是我在 Razor 视图中的代码,它基本上通过从数据库中提取信息来显示表格 -

@model List<EmpoyeeInfo.Models.FFX_HR_Employees>
@using System.Reflection;

@{
    ViewBag.Title = "Employee Information";
    var Properties = Model[0].GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance).ToList();
    string[] head = new string[Properties.Count()];
}

<div id="web-top">

    <div id="horizontal-line"></div>
    <input class="search-box-text" type="text" spellcheck="false" placeholder="Search Individual Record..." title="Search Individual Record" id="searchbox" name="searchbox" />

</div>

<div id="web-main">
    <table class="employee-info">
        <tr>
            @foreach (var Property in Properties)
            {
                if (Property.Name.Equals("AnnualHolidayEntitlement"))
                {
                    <th colspan="2">@Property.Name</th>
                }
                else
                {
                    <th>@Property.Name</th>
                }
            }
        </tr>

        @foreach(var Row in Model)
        {
            <tr>
                @{
                    Type type = Row.GetType();
                    IList<PropertyInfo> props = new List<PropertyInfo>(type.GetProperties());
                }
                @foreach (PropertyInfo prop in props)
                {
                    if (prop.Name.Equals("AnnualHolidayEntitlement"))
                    {
                        <td contenteditable="true">@prop.GetValue(Row, null)</td>
                    }
                    else
                    {
                        <td>@prop.GetValue(Row, null)</td>
                    }
                }
                <td class="saveToDB">SAVE</td>
            </tr>
        }
    </table>
</div>

但是当我在搜索框中输入文本时,会进行 ajax 调用 -

$(document).ready(function () {
    $('.search-box-text').keypress(function () {
        getReport($(this).html());
    });
})

function getReport(Name) {
    $.ajax({
        url: '@Url.Action("Index", "Home")',
        type: 'POST',
        data: { Name: Name },
        dataType: "json",
        cache: false,
        success: function (result) {
            //do stuff;
        },
        error: function () {
            console.log("no display");
        }
    });
}

现在我如何重新加载 div -“web-main”并更新 Model 值,这样当用户搜索名称时,表格也需要更新。

【问题讨论】:

    标签: javascript c# asp.net ajax razor


    【解决方案1】:

    下面的代码会将结果附加到 div 'web-main'。您需要在代码中操作 jQuery 的成功部分

    $(document).ready(function () {
              $('.search-box-text').keypress(function () {
                  getReport($(this).html());
              });
          })
    
          function getReport(Name) {
              $.ajax({
                  url: '@Url.Action("Index", "Home")',
                  type: 'POST',
                  data: { Name: Name },
                  dataType: "json",
                  cache: false,
                  success: function (data) {
                      //do stuff;
          console.log(data);
                    $("web-main").append(JSON.stringify(data));
                  },
                  error: function () {
                    console.log("no display");
                  }  
              });
          }
    

    【讨论】:

    • 有没有办法改变模型的值?类似 - @Model = data(在脚本部分)
    • 您应该在 Index 操作中操作 HomeController 中的数据。从 index 操作返回值后,您可以将 html 附加到 div
    猜你喜欢
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2021-06-21
    • 1970-01-01
    • 2021-08-13
    • 2013-07-02
    • 2021-07-23
    • 1970-01-01
    相关资源
    最近更新 更多