Codehelp
Codehelp

Reputation: 4747

Submitting a form to ASP.NET MVC from Knockout does not bring in all the values

Here is what I have in my view in ASP.NET MVC 5

@model Entities.Coupon

@using (Html.BeginForm("coupon", "marketing", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="scsm-18 scmd-16 sclg-14">
        <div class="form-group">
            <label>Codes</label>
            @Html.TextBoxFor(p => p.Code, new { @class = "form-control", @data_bind = "value: Code", @autofocus = true, @maxlength = "50" })
        </div>
        <input type="radio" name="IsPerCentOrDollar" value="1" data-bind="checked: IsPerCentOrDollar" />
        <span>PercentageAmount</span>
        <input type="radio" name="IsPerCentOrDollar" value="2" data-bind="checked: IsPerCentOrDollar" />
        <span>DollarAmount</span>
        <input type="radio" name="IsPerCentOrDollar" value="3" data-bind="checked: IsPerCentOrDollar" />
        <span>FreeShipping</span>
    </div>

    <div class="panel-footer text-right">
    <input type="submit" name="commandType" id="btnSave" class="btn btn-primary" data-bind="click:submit" value="Save" />
    </div>
}

In the script:

$(document).ready(function () {
    var viewModel = new CouponViewModel(couponModel);
    ko.applyBindings(viewModel);

    function CouponViewModel(data) {
    self.Code = ko.observable(data.Code);
    self.IsPerCentOrDollar = ko.observable("1");
    self.DiscountLevel = ko.computed(function () {
        return self.IsPerCentOrDollar();
        });
    };
}

Code in MVC:

[HttpPost, ActionName("coupon")]
public ActionResult coupon(Coupon coupon)
        {
            try
            {
                // some logic not yet in
            }
            catch (Exception ex)
            {

            }
            return View();
        }

That's all I have in there now.

In Developer tools inside the browser I can see values for self.DiscountLevel change on the selection of radio buttons.

On Submit, at MVC front the value of Code comes in but the values for DiscountLevel are not.

Any help is greatly appreciated.

Regards.

Upvotes: 1

Views: 1216

Answers (1)

Jeroen
Jeroen

Reputation: 63719

Let me expand on @StephenMuecke's comment (which has the gist of it I think).

ASP.NET MVC's default model binding will fill the argument (Coupon) with values found in the request. Only form elements are sent along with the request. You seem to expect that DiscountLevel is sent along, but it's just a JavaScript function that exists in the user's browser.

Adding something like this may solve your immediate problem:

<input type="hidden" name="DiscountLevel" data-bind="value: DiscountLevel" />

To note a related issue though: the property you have trouble with is a computed observable. However, you probably do not want to send it along as it depends entirely on IsPerCentOrDollar. Just have your server side Coupon class derive the discount level from that property too. That would also prevent users from hacking the hidden input and sending in a malicious value.

Upvotes: 3

Related Questions