Souro says:
$("#ide").val(run.EmailId);
$("#nm").val(run.Name);
Instead of val property you have to use html property to assign the value to the td.
$("#ide").html(run.EmailId);
$("#nm").html(run.Name);
Check the below sample for reference.
Database
CREATE TABLE Users(
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [varchar](50) NOT NULL,
[Email] [nvarchar](50) NOT NULL
)
GO
INSERT INTO Users VALUES('Jacob','jacob@bngg.com')
Controller
public class HomeController : Controller
{
// GET: /Home/
public ActionResult Index()
{
return View();
}
public JsonResult Search(string email)
{
UsersEntities entities = new UsersEntities();
var str = (from test in entities.Users where test.Email == email select test).FirstOrDefault();
return Json(str, JsonRequestBehavior.AllowGet);
}
}
View
<body>
<form class="form-inline">
<div class="form-group mx-sm-3 mb-2">
<label for="exampleInputEmail1">
Email</label>
<input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp"
placeholder="Enter email">
</div>
<button type="button" id="btnsearch" class="btn btn-primary mb-2">
Search</button>
</form>
<div>
<div class="container" id="tbleshow">
<table class="table table-striped">
<tr>
<th>
ID
</th>
<th>
Name
</th>
</tr>
<tr>
<td id="ide">
</td>
<td id="nm">
</td>
</tr>
</table>
</div>
</div>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#tbleshow").hide();
$("#btnsearch").click(function (e) {
e.preventDefault();
$("#tbleshow").show();
return myfunction();
});
function myfunction() {
var model = { Email: $("#exampleInputEmail1").val() }
$.ajax({
type: 'GET',
url: "/Home/Search",
dataType: 'json',
data: { Email: model.Email },
success: function (run) {
$("#ide").html(run.Email);
$("#nm").html(run.UserName);
if (run) {
alert("This is the details");
}
else {
alert("Something wrong in success");
}
},
error: function () {
console.log('something went wrong - debug it!');
}
});
}
});
</script>
</body>
Screenshot