获取Id以通过jquery更新值

时间:2016-07-17 15:07:49

标签: javascript jquery asp.net-mvc

我有一个非常简单的页面,显示问题的答案。 当用户点击"有用"或"无用"按钮,我想得到答案的ID并使用jQuery在DB中更新有用或无用的字段。我的问题是如何使用jQuery获取每个AnswerId?

这是AnswerModel:

   public class Answer
   {
       public int AnswerId { get; set; }

       [Required]
       [StringLength(500)]
       public string Reply { get; set; }

       public int Useful { get; set; }

       public int Useless { get; set; }
    }

这是AnswerController:

     public async Task<ActionResult> Index()
    {
        return(await db.Answers.toListAsync());
    }

    public async Task<JsonResult> Helpful(int? ansId, int? useful, int? uselees)
    {
        Answer model = await db.Answers.FindAsync(ansId);
        if (model != null)
        {
            model.Useful  = model.Useful + (int)useful;
            model.Useless = model.Useless + (int)useless;
            db.Entry(model).State = EntityState.Modified;
            await db.SaveChangesAsync();
            return Json(new { Success = true }, JsonRequestBehavior.AllowGet);
        }
        return Json(new { Success = false }, JsonRequestBehavior.AllowGet);
    }

这是索引视图:

    @model IEnumerable<project.Models.Answer>

    @foreach (var item in Model)
    {
        <table class="table">
        <tr>
            <td>
                Reply: @item.Reply
            </td>                
            <th>
                <input type="button" id="usfBtn" value="Useful" />
            </th>
            <th>
                <input type="button" id="uslBtn" value="Useless" />
            </th>
        </tr>
        </table>
    }

以下是剧本:

    $(function() {
               $("#usfBtn").click(function () {        
                    Usf(Alarm);
                 });
               $("#uslBtn").click(function () {
                   Usl(Alarm);
                });
                function Usf(callback) {
                $.ajax(
                {
                      type: "Get",
                      url: "/Answer/Helpful",
                      data: {
                              useful: 1,
                              useless: 0,
                            },
                      dataType: "json",
                      success: function (result) {
                      callback(result);
                  }
               });
              }
              function Usl(callback) {
                 $.ajax(
                 {
                     type: "Get",
                     url: "/Answer/Helpful",
                     data: {
                            useful: 0,
                            useless: 1,
                     },
                     dataType: "json",
                     success: function (result) {
                         callback(result);
                     }
               });
            }
            function Alarm(arg) {
                 if (arg.Success) {
                        window.location.href = window.location.pathname;
                 }
            }
  });

我的问题是如何获得每个答案的ID,而我只有一个按钮ID(&#34; usfBtn&#34;或&#34; uslBtn&#34;)?

1 个答案:

答案 0 :(得分:0)

您可以将答案ID保留为按钮的数据属性。

<table class="table">
@foreach (var item in Model)
{ 
    <tr>
        <td>
            Reply: @item.Reply
        </td>                
        <th>
            <input type="button" data-aid="@item.Id" class="u" 
                        data-mode="useful"  value="Useful" />
        </th>
        <th>
            <input type="button" data-aid="@item.Id" class="u" 
                       data-mode="useless" value="Useless" />
        </th>
    </tr>
}
</table>

现在读取数据属性值并将其发送到服务器

$(function(){

  $("input.u").click(function(e){
     e.preventDefault();
     var s= { ansId : $(this).data("aid"), useful :0, useless:0 };
     if($(this).data("mode")=="useful")
     {
       s.useful = 1;
     }
     else if($(this).data("mode")=="useless")
     {
       s.useless = 1;
     }
     //post this object now
     $.post("/Answer/Helpful",s,function(res){
        //do something with response.
     });

  });

});