chamara
chamara

Reputation: 12711

Get Label value in MVC 2

I want to print some text on dropDown change. then on submit save the label text to data base. Currently printing value to the label works fine but on submit i'm not receiving the lable text.

  $(document).ready(function () {
 $('#StockID').change(function () {

// ajax call
 function successFunc(data, status) {
 $("#lblTotal").text("Stock Value: " + data.Result);
 }

}
})
});

 <div class="editor-field">
               <%: Html.DropDownListFor(x => x.StockID, new SelectList(Model.lstStock, "StockID", "Description"), "-- Please Select a Stock --")%>
                 <%: Html.ValidationMessageFor(model => model.StockID)%>
            </div>

            <div id="clslbl">
            <br />
                <label id="lblTotal"></label>

            </div>

Controller:

if (ModelState.IsValid)
                    {// TODO: Add insert logic here

                        string a = Request.Form["lblTotal"]; // here i'm not getting the label text

                        return RedirectToAction("Index");
                    }

Upvotes: 0

Views: 1111

Answers (1)

PSL
PSL

Reputation: 123739

Labels are not posted back to the server same is the case if you use Html.DisplayFor(...). As you change the drop-down value save it in a hidden fields as well. You will be able to access it as part of Request on the server. Only input fields are posted back to the server. So <input type="hidden" .../> should do the job for you.

Upvotes: 1

Related Questions