Hi,
I have a label used in my view page as below
@Html.Label("label1")
I want to change the text of a label from the controller's action method. It shoud be something like the below code which we can set in the asp.net webform
//from the code behind function
label1.Text="new text";
Can some one please check and help me to resolve the issue?

Tahir AnsariRank #366 · 6.8K reputationPosted Aug 16, 2023, 9:17 AM EDT
public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult UpdateLabelText(string selectedValue) { string updatedText = "Updated text based on " + selectedValue; return Content(updatedText); } }@{ ViewBag.Title = "Dynamic Label Text Example"; } Dynamic Label Text Example @Html.DropDownList("dropdown", new SelectList(new[] { "Option 1", "Option 2", "Option 3" })) $(document).ready(function () { $("#dropdown").change(function () { var selectedValue = $(this).val(); $.ajax({ type: "GET", url: "@Url.Action("UpdateLabelText", "Home")", data: { selectedValue: selectedValue }, success: function (data) { $("#label1").text(data); // Update the label's text } }); }); });Tahir AnsariRank #366 · 6.8K reputationPosted Aug 16, 2023, 9:05 AM EDT
If you are using asp.net mvc
public ActionResult YourAction() { ViewBag.LabelText = "new text"; // Set the new text return View(); }Your view
Anu200 reputationPosted Aug 16, 2023, 9:12 AM EDT
Thanks for your reply, the method which you suggest will work in ach page load, but in my case the action method with get involked each time when I am changing the selection in the dropdown. At the same time I want to show the updated text in the label also.
Anu200 reputationPosted Aug 16, 2023, 9:54 AM EDT
Thanks, it worked