csdn产品小助手 2016-06-01 18:10 采纳率: 0%
浏览 31

使用ajax更新列表。

I am currently trying to update a list of duties after the user has posted a new duty. I am trying to use Ajax doing this. However I am not shure how to use ajax for posting to my controller and then add to my list. How do I use my datafield in my ajax method and how do I update my list?

My controller:

        [HttpPost]
        Public ActionResult CreateDuty(PartialViewModel d)
{
        db.DutiesTable.Add(d.OneDuty);
                    db.SaveChanges();
                    pv.DutyList = db.DutiesTable.ToList();

                    return View("Index");
    }

My Ajax method:

<script>
    $('#getDuty').click(function () {
        $.ajax({
            type: 'POST',
            url: "/Home/CreateDuty",
            data: {d : d}
        }).success(function (data) {
            alert(data + status);
        });
    });
</script>
  • 写回答

1条回答 默认 最新

  • weixin_33682790 2016-06-01 18:33
    关注

    Not enough information. Where did d come from and what is it (javascript).

    For your EF code I assume DutiesTable does NOT contain PartialViewModel's so there is a mapping that needs to happen there between the view model and EF model classes.

    [HttpPost]
    Public ActionResult CreateDuty(DutyViewModel d)
    {
        Duty newduty = new Duty(){ /*map values from d to fields here*/}
        db.Duties.Add(newduty);
        db.SaveChanges();
        return View("Index");
    }
    

    Once you have your data posting over the controller and saving with EF you need to decide on if your javascript needs a copy of the updated entity (mapped back to a PartialViewModel most likely or if a simple success/fail message is enough and do what it needs to on that side. I don't know what you mean here: "and then add to my list" - what list?

    评论

报告相同问题?

悬赏问题

  • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
  • ¥15 webots有问题,无响应
  • ¥15 数据量少可以用MK趋势分析吗
  • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
  • ¥15 大智慧怎么编写一个选股程序
  • ¥100 python 调用 cgps 命令获取 实时位置信息
  • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
  • ¥15 C语言使用vscode编码错误
  • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
  • ¥20 ensp怎么配置让PC1和PC2通讯上