5

I am trying to send value of StockDate to Controller Method. I am getting Null. http://knockoutmvc.com/ParametersToServer

as per above link if I pass static value like below.I can get value but when I use Model.Property I always get Null.

@ko.Html.Button("Inc 3", "Increment", "ParametersToServer", new { value = 3 })

Following is the code..

View Code-

    @using PerpetuumSoft.Knockout
@model OpManWeb.ViewModel.Inventory.TransactionHistory

@{
    var ko = Html.CreateKnockoutContext();
}
@{
    ViewBag.Title = "Add Stock";
}


<script src="~/Scripts/perpetuum.knockout.js"></script>
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>

@using (ko.Html.Form("SaveStock", "Inventory"))
{
    <div class="row">
        <div class="col-md-3 col-lg-3 clmargin">
                <div class="form-group  col-md-4 zeropadding div2adjustments">
                    @Html.LabelFor(m => m.StockDate, new { @class = "fieldtext" })
                </div>
                <div class="col-md-8 div2adjustments">
                    @ko.Html.TextBox(m => m.StockDate, new { @class = "form-control input-sm fieldtextinput ", 
                @id="stockdatepicker" })
                </div>
            </div>
    </div>

     @ko.Html.Button("Cancel", "sample1", "Inventory", new { value = Model.StockDate}, new { @class = "btn btn- primary maxheightbtn col-md-12", @id = "btn" })
}

Model Code-

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace OpManWeb.ViewModel.Inventory
{
    public class TransactionHistory
    {
        [DisplayName("Stock Date")] 
        public string StockDate
        {
            get;
            set;
        }
    }
}

Controller Code-

   public ActionResult sample1(string value)
{
    return Json(value);        
}

I always get null for string value in controller.. Can anyone have faced such issue..? Help..!! Thanks In Advance..!!

Aditya Raval
  • 590
  • 4
  • 20

1 Answers1

0

You can only send back extra static parameters with @ko.Html.Button.

However you can get your whole client side model object (this is always sent by K-MVC) inside your controller action with adding a model parameter to it.

So you can get the current model.StockDate with modifying your controller action to:

public ActionResult sample1(TransactionHistory model)
{
    return Json(model.StockDate);        
}
nemesv
  • 138,284
  • 16
  • 416
  • 359