AJAX不会被触发,asp.net [英] AJAX does not get triggered, asp.net

查看:46
本文介绍了AJAX不会被触发,asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在它只将AuctionId值输入到控制器中,其他属性为null或0 ...

Now it only get the AuctionId value in to the controller the other props are either null or 0...

这是表格:

<form id="createBid">
        <div id="frmBid" class="form-inline">
            <input name="Bidder" asp-for="@bidModel.Bidder" value="@User.Identity.Name" type="hidden" />
            <input name="AuctionId" asp-for="@bidModel.AuctionId" value="@Model.AuctionId" type="hidden" id="auctionId" />
            <label asp-for="@bidModel.Amount" />
            <input name="Amount" asp-for="@bidModel.Amount" />
            <button type="submit" id="submitBtn" class="btn btn-primary">Lägg</button>
        </div>
    </form>

这是控制器中的动作:

public async Task<IActionResult> AddBid(BidModel Bid)
{
    var result = await _bidBusinessInterface.CreateBidAsync(Bid, Bid.AuctionId);
    if (result)
    {
        ViewBag.Message = "Bud lagt!";

    }
    else
    {
        ViewBag.Message = "Bud förlågt!";
    }
    return RedirectToAction("ViewDetails");
}

然后我们进行了实际的AJAX调用:

And then we have the actual AJAX call:

    $('#createBid').on('submit', function (e)
    {
        e.preventDefault();

        var $form = $(this);
        $.ajax({
            url: '@Url.Action("AddBid")',
            type: 'POST',
            dataType: 'html',
            data: $form.serialize(),
            success: function (html)
            {
                $('#frmBid').html(html);
            }
        });
});

如果需要查看模型出问题的地方,我也会在此处发布模型:

I'm posting the model here aswell, if it is needed to see where it goes wrong:

public class BidModel
{
    [JsonProperty("BudID")]
    public string BidId { get; set; }
    [JsonProperty("Summa")]
    public int Amount { get; set; }
    [JsonProperty("AuktionID")]
    public string AuctionId { get; set; }
    [JsonProperty("Budgivare")]
    public string Bidder { get; set; }
}

我对每一个答案都很感激!这已经困扰了我两个小时.

I'm very grateful for every answer! This has been bugging me for 2 hours..

推荐答案

您使用的asp帮助程序有误.

You are using your asp helpers wrong.

让我们看一下这段代码示例:

let's take this sample of code:

<输入名称=投标人" asp-for ="@ bidModel.Bidder" value ="@ User.Identity.Name" type ="hidden"/>

您将 name 设置为"Bidder",这是数据将要绑定的内容,当您需要"Bid.Bidder"时,因为"Bid"是操作接收到的对象的名称.

you set the name to "Bidder" this is what the data will be bound to, when you need "Bid.Bidder" since "Bid" is the name of the object that the action receives.

现在您无需设置 name 属性,因为 asp-for 助手会为您完成此操作,它将自动为您生成name,id属性.

Now you DO NOT need to set the name attribute since asp-for helper does it for you, it will automatically generate name, id attributes for you.

现在确保在页面顶部具有 @model YourNamespace.BidModel

now make sure at the top of you page you have @model YourNamespace.BidModel

并像这样引用它:

<输入asp-for ="@ Model.Bidder" value ="@ User.Identity.Name" type ="hidden"/>

属性将自动生成,并且模型应正确绑定.

the attributes will be generated automatically and the model should be bound correctly.

这篇关于AJAX不会被触发,asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆