Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. FILL HTML.EDITFOR FIELDS ON BUTTON CLICK IN ASP.NET MVC

FILL HTML.EDITFOR FIELDS ON BUTTON CLICK IN ASP.NET MVC

Scheduled Pinned Locked Moved ASP.NET
asp-netcsharpjavascripthtmltools
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pathllk
    wrote on last edited by
    #1

    Dear Friends I am new MVC world & working in a project. I want to fill some html.editfor fields on a button click. My tried the given below codes: Controller action:

    [HttpPost]
    public JsonResult GetRecordById(string id)
    {
    var record = dc.TABLE1.FirstOrDefault(x => x.PART_NO == id);
    var result = new
    {
    Name = record.PARTI,
    Type = record.TYPE
    };
    return Json(result, JsonRequestBehavior.AllowGet);
    }

    VIEW:

    <script type="text/javascript">
    $(function () {
    $("#txtParti").autocomplete({
    source: '@Url.Action("GetParti")'
    });
    });
    $(function () {
    $("#txtPtno").autocomplete({
    source: '@Url.Action("GetPtno")'
    });
    });
    $('#BTNFET').onclick(function (fetrec) {
    var recordId = this.value;
    $.post('@Url.Action("GetRecordById")',
    {
    "id": recordId
    },
    function (data) {
    $("#txtParti").val(data.Name);
    $("#Type").val(data.Type);
    });
    });
    </script>

    <input type="button" id="BTNFET" onclick="fetrec();" />
    @Html.EditorFor(model => model.PARTI, new { htmlAttributes = new { @class = "form-control", @id = "txtParti", @placeholder = "PART NAME" } })
    @Html.EditorFor(model => model.PART_NO, new { htmlAttributes = new { @class = "form-control", @id = "txtPtno", @placeholder = "PART NO" } })
    @Html.EditorFor(model => model.TYPE, new { htmlAttributes = new { @class = "form-control", @id = "Type", @placeholder = "ITEM TYPE" } })

    On the above codes Auto complete part working pefectly, Button click not working. So, friends please help me in this regard.

    N 1 Reply Last reply
    0
    • P pathllk

      Dear Friends I am new MVC world & working in a project. I want to fill some html.editfor fields on a button click. My tried the given below codes: Controller action:

      [HttpPost]
      public JsonResult GetRecordById(string id)
      {
      var record = dc.TABLE1.FirstOrDefault(x => x.PART_NO == id);
      var result = new
      {
      Name = record.PARTI,
      Type = record.TYPE
      };
      return Json(result, JsonRequestBehavior.AllowGet);
      }

      VIEW:

      <script type="text/javascript">
      $(function () {
      $("#txtParti").autocomplete({
      source: '@Url.Action("GetParti")'
      });
      });
      $(function () {
      $("#txtPtno").autocomplete({
      source: '@Url.Action("GetPtno")'
      });
      });
      $('#BTNFET').onclick(function (fetrec) {
      var recordId = this.value;
      $.post('@Url.Action("GetRecordById")',
      {
      "id": recordId
      },
      function (data) {
      $("#txtParti").val(data.Name);
      $("#Type").val(data.Type);
      });
      });
      </script>

      <input type="button" id="BTNFET" onclick="fetrec();" />
      @Html.EditorFor(model => model.PARTI, new { htmlAttributes = new { @class = "form-control", @id = "txtParti", @placeholder = "PART NAME" } })
      @Html.EditorFor(model => model.PART_NO, new { htmlAttributes = new { @class = "form-control", @id = "txtPtno", @placeholder = "PART NO" } })
      @Html.EditorFor(model => model.TYPE, new { htmlAttributes = new { @class = "form-control", @id = "Type", @placeholder = "ITEM TYPE" } })

      On the above codes Auto complete part working pefectly, Button click not working. So, friends please help me in this regard.

      N Offline
      N Offline
      Nathan Minier
      wrote on last edited by
      #2

      It looks like you have more of a JavaScript issue than anything else. First, remove the onclick handler for the HTML tag, it's extraneous and basically binds a null event to the button I don't see a function fetrec defined anywhere, just an anonymous event handler.

      Now for the JS. I'm not 100% sure about best practices with jQuery, and $.ajax() might be a better route, but we'll roll with what you have for the moment:

      $('#BTNFET').onclick(function (e) {
      $.post('@Url.Action("GetRecordById")',
      {
      "id": e.target.value
      },
      function (data) {
      $("#txtParti").val(data.Name);
      $("#Type").val(data.Type);
      });
      });

      Now, in this context the 'this' object should be the event initiator, but that's not a guarantee. The handler will automatically be passed the event as an argument, though, and we can use that to hook the calling HTML element via the 'target' property. This has the benefit of not leaning on 'this' and therefore not needing to add a new variable into the mix.

      D 1 Reply Last reply
      0
      • N Nathan Minier

        It looks like you have more of a JavaScript issue than anything else. First, remove the onclick handler for the HTML tag, it's extraneous and basically binds a null event to the button I don't see a function fetrec defined anywhere, just an anonymous event handler.

        Now for the JS. I'm not 100% sure about best practices with jQuery, and $.ajax() might be a better route, but we'll roll with what you have for the moment:

        $('#BTNFET').onclick(function (e) {
        $.post('@Url.Action("GetRecordById")',
        {
        "id": e.target.value
        },
        function (data) {
        $("#txtParti").val(data.Name);
        $("#Type").val(data.Type);
        });
        });

        Now, in this context the 'this' object should be the event initiator, but that's not a guarantee. The handler will automatically be passed the event as an argument, though, and we can use that to hook the calling HTML element via the 'target' property. This has the benefit of not leaning on 'this' and therefore not needing to add a new variable into the mix.

        D Offline
        D Offline
        deepankarbhatnagar
        wrote on last edited by
        #3

        Nice answer +5

        hi

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups