Linking button to controller using jquery and ajax (url) -


i'm trying create button database lookup value in mvc project. i'm bit stuck after reading round tips.

actual cshmtl create

  odel oncologyrta.controllers.oncologypatient  @{     viewbag.title = "create"; }  <h2>add new patient</h2>   @using (html.beginform()) {     @html.antiforgerytoken()      <button type="button" name="button" value="find">search</button>       <div class="form-horizontal">         <h4><font color="red"><strong>live dataset</strong></font></h4>         <hr />         @html.validationsummary(true)          <dl class="dl-horizontal">             <dt>                 @html.labelfor(model => model.hospitalid, new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.textboxfor(model => model.hospitalid, new { onkeyup = "inputtoupper(this);"})                 @html.validationmessagefor(model => model.hospitalid)             </dd>              <dt>                 @html.labelfor(model => model.nhs_no_, "nhs no.", new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.editorfor(model => model.nhs_no_)                 @html.validationmessagefor(model => model.nhs_no_)             </dd>              <dt>                 @html.labelfor(model => model.forename, new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.textboxfor(model => model.forename, new { onkeyup = "inputtoupper(this);", name="forename"})                 @html.validationmessagefor(model => model.forename)             </dd>              <dt>                 @html.labelfor(model => model.surname, new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.textboxfor(model => model.surname, new { onkeyup = "inputtoupper(this);" })                 @html.validationmessagefor(model => model.surname)             </dd>              <dt>                 @html.labelfor(model => model.dob, new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.textboxfor(model => model.dob, new { @value = "dd/mm/yyyy" })                 @html.validationmessagefor(model => model.dob)             </dd>              <dt>                 @html.labelfor(model => model.active, new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.editorfor(model => model.active)                 @html.validationmessagefor(model => model.active)             </dd>              <dt>                 @html.labelfor(model => model.deceased, new { @class = "control-label col-md-2" })             </dt>              <dd>                 @html.editorfor(model => model.deceased)                 @html.validationmessagefor(model => model.deceased)             </dd>          </dl>            <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="create" class="btn btn-default" />             </div>         </div>     </div> }   <script>     function inputtoupper(obj) {         if (obj.value != "") {             obj.value = obj.value.touppercase();         }     } </script>  <script>     $("button[name='button']").click(function (event) {         event.preventdefault();         var uquery = $('#hospitalid').val();         var url = '@url.action("getdata")';         var data = { value: uquery }         $.ajax({             url: url,             data: data,             success: function (data) {                 $('#forename').val(data.hospitalid);             }         });     }) </script>    <div>     @html.actionlink("back list", "index") </div>  @section scripts {     @scripts.render("~/bundles/jqueryval") } 

actual code class

>  public class lkup >     { >         public string hospitalid { get; set; } >         public string nhs_no_ { get; set; } >         public string forename { get; set; } >         public string surname { get; set; } >         public system.datetime dob { get; set; } >     } 

httpget code in controller

[httpget]         public jsonresult getdata(string value)         {             var result = new lkup();              string connect = "server=server\\instance;database=sqltable;trusted_connection=true";             string query = "select * table1 pkey =" +value;              using (sqlconnection sql1 = new sqlconnection(connect))             {             sql1.open();             sqlcommand cmd = new sqlcommand(query);             sqldatareader rdr = cmd.executereader();             {       if (rdr != null)                     {                         while (rdr.read())                         {                             result.hospitalid = "column1";                             result.forename = "column2";                             result.surname = "colum3";                         }                     }             rdr.close();             }             sql1.close();             }              return json(result, jsonrequestbehavior.allowget);         } 

***** update *****

i think code fine, we've tested javascript alerts , pulling correct field , data. problem seems in ajax call gets 404 when trying route through controller. this:

get http://localhost:59845/oncologypatientscontroller/getdata?id=1234567 404 (not found) send @ jquery-1.10.2.min.js:23 x.extend.ajax @ jquery-1.10.2.min.js:23 (anonymous function) @ create:157 x.event.dispatch @ jquery-1.10.2.min.js:22 v.handle @ jquery-1.10.2.min.js:22 

what following actionmethods url in js code:

<button type="button" value="find">find</button> <script>     $("button").click(function(event)     {         event.preventdefault();         var uquery = $('hid').val();         var url = '@url.action("getdata","controllername")';         var data = { value: uquery }         $.ajax({             url: url,             data: data,             success : function(data)              {                 $('hid').val(data.value1);             }         });}) 

but hackerman mentioned, if view belongs controller can do:

@url.action("getdata") 

Comments

Popular posts from this blog

r - how do you merge two data frames the best way? -

How to debug "expected android.widget.TextView but found java.lang.string" in Android? -

php - mySQL problems with this code? -