1

I'm trying to pass an object through a form to my controller with an AJAX call.

Here is the object, everything returns null/0 except AuctionId:

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; }
}

The Form:

<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>

Here is the AJAX call:

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

And then we have the action in the controller:

[HttpPost]
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", Bid.AuctionId);
}

So the problem is, that some value, not all values returns null.

Why isn't AuctionId null but the others are?

I also tried to make a new ViewModel, since I already had an Auction as Viewmodel in the view. I made a new with Auction and a Bid And i make the form look like this:

<form id="createBid">
            <div id="frmBid" class="form-inline">
                <input asp-for="BidVM.AuctionId" value="AuctionVM.AuctionId" type="hidden" id="auctionId" />
                <label asp-for="BidVM.Amount" />
                <input asp-for="BidVM.Amount" />
                <button type="submit" id="submitBtn" class="btn btn-primary">Lägg</button>
            </div>
        </form>

But now everything is null

4
  • You already asked this question here: stackoverflow.com/questions/52336284/… Commented Sep 15, 2018 at 10:10
  • Yes, but i editerad that question, and there was no more readers. So I figured if i make a new one with the cortege text people Will read it Commented Sep 15, 2018 at 10:12
  • Try to remove the JsonProperty in order to simplify the problem. Commented Sep 15, 2018 at 10:33
  • But I need them when Maping in from the database, don’t i? Commented Sep 15, 2018 at 10:34

1 Answer 1

1

This works fine after some code cleanup.

Client side - View and JS

@model MyNamespace.BidModel

@{
    // Assuming you need to use local variable rather than just @model of BidModel type
    // If you can just use model - you will be able to bind imputs like this: 
    // <input name="@nameof(Model.BidId)" value="@Model.BidId" type="hidden" />
    // or
    // <input name="BidId" value="@Model.BidId" type="hidden" />
    // or (even better)
    // just use the tag helper asp-for like this: 
    // <input asp-for="BidId" type="hidden" />

    var bidModel = Model;
}

<form id="createBid">
    <div id="frmBid" class="form-inline">
        <input name="@nameof(bidModel.Bidder)" value="@bidModel.Bidder" type="hidden" />
        <input name="@nameof(bidModel.AuctionId)" value="@bidModel.AuctionId" type="hidden" />
        <input name="@nameof(bidModel.BidId)" value="@bidModel.BidId" type="hidden" />

        <input name="@nameof(bidModel.Amount)" value="@bidModel.Amount" />

        <button type="submit" id="submitBtn" class="btn btn-primary">Add</button>
    </div>
</form>

<script type="text/javascript">
    $(document).ready(function() {

        $('#createBid').on('submit',
            function(e) {
                e.preventDefault();
                var $form = $(this);
                $.ajax({
                    url: '@Url.Action("AddBid")',
                    type: 'POST',
                    data: $form.serialize(),
                    success: function(html) {
                        $('#frmBid').html(html);
                    }
                });
            });
    });
</script>

Server side - Controller and Model

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace MyNamespace
{
    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; }
    }

    public class BidController : Controller
    {
        [HttpGet]
        public async Task<IActionResult> AddBid()
        {
            // For demo purposes - pre-fill some values
            var model = new BidModel
            {
                Bidder = User.Identity.Name,
                Amount = 123,
                AuctionId = "A-ID",
                BidId = "B-ID"
            };

            return View(model);
        }

        [HttpPost]
        public async Task<IActionResult> AddBid(BidModel Bid)
        {
            // save new bid
            // return RedirectToAction("ViewDetails", Bid.AuctionId);

            return View(Bid);
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.