我有下面的控制器和方法。一个带参数,另一个不带。我希望能够进行搜索。
- 当我点击提交按钮时。什么都没发生。我的ajax调用没有命中。
- 如果问题 1 得到解决,我希望能够输入我的搜索条件并让它返回我的索引视图中使用的现有表中的数据。
请协助。Ajax 和 mvc 的新手。
public class HomeController : Controller
{
public ActionResult Index()
{
//List<Product> myProductList = GetAllProducts();
//return View(myProductList);
//List<Product> myProductList = GetAllProducts();
return View();
}
public ActionResult About()
{
return View();
}
public List<Product> GetAllProducts()
{
string myConnect = ConfigurationManager.ConnectionStrings["ConnectSir"].ConnectionString;
List<Product> prdResults = new List<Product>();
SqlConnection con = new SqlConnection(myConnect);
SqlCommand cmd = new SqlCommand("select * from products",con);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Product newProduct = new Product();
newProduct.Id = Convert.ToInt16(reader["Id"]);
newProduct.Name = reader["Name"].ToString();
newProduct.Description = reader["description"].ToString();
newProduct.Price = Convert.ToDecimal(reader["Price"]);
newProduct.UnitsInStock = Convert.ToInt16(reader["UnitsInStock"]);
prdResults.Add(newProduct);
}
}
return prdResults;
}
[HttpPost]
public JsonResult GetAllProducts(string searchName)
{
string myConnect = ConfigurationManager.ConnectionStrings["ConnectSir"].ConnectionString;
List<Product> prdResults = new List<Product>();
string sqlcmd = @"select * from products where name = @name";
SqlConnection con = new SqlConnection(myConnect);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = sqlcmd;
cmd.Parameters.Add("@name", SqlDbType.NVarChar);
cmd.Parameters["@name"].Value = searchName;
cmd.Parameters["@name"].Direction = ParameterDirection.Input;
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Product newProduct = new Product();
newProduct.Id = Convert.ToInt16(reader["Id"]);
newProduct.Name = reader["Name"].ToString();
newProduct.Description = reader["description"].ToString();
newProduct.Price = Convert.ToDecimal(reader["Price"]);
newProduct.UnitsInStock = Convert.ToInt16(reader["UnitsInStock"]);
prdResults.Add(newProduct);
}
}
//return prdResults;
return Json(prdResults);
}
}
*********************************
Html
@model IEnumerable<MvcAjax.Models.Product>
<script src="../../Scripts/jquery-1.10.2.js" type="text/javascript"></script>
<form>
<div>
<input type="text" name="search" id="searchItem" />
<input type="submit" value="Retrieve" id="btnSearch"/>
</div>
<div>
</div>
<table id="items">
<tr>
<th></th>
<th>
Name
</th>
<th>
Description
</th>
<th>
Price
</th>
<th>
UnitsInStock
</th>
</tr>
</table>
<script type="text/javascript">
$('#btnSearch').click(function () {
$.ajax({
url: 'Home/GetAllProducts/',
type: 'POST',
dataType: 'json',
data: { searchName: $('#searchItem').val() }
}).done(function (data) {
if (data && data.length) {
for (var i = 0; i < data.length; i++) {
var newTR = '<tr>';
//create your TR, such as
newTR += '<td>' + data[i].Name + '</td>';
newTR += '<td>' + data[i].Description + '</td>';
newTR += '<td>' + data[i].Price + '</td>';
newTR += '<td>' + data[i].UnitsInStock + '</td>';
//and so on...
newTR += '</tr>';
$('#items > tr:last').append(newTR);
}
}
});
});
</script>
</form>