带有WHERE子句和来自视图的两个参数的c# MVC查询
本文关键字:两个 参数 查询 MVC 子句 WHERE 视图 带有 | 更新日期: 2023-09-27 17:50:12
我需要用WHERE子句查询我的数据库,并将参数从我的视图传递到控制器。我在一个总的损失,因为我是一个JavaScript和PHP开发人员,目前正在学习。net MVC(我使用vs10 framework4)。
我需要在本质上做这个查询:
select * from myTable where column1=16 and column2=24
column1和column2的值在我的视图中。我已经找到了一百万个关于如何拉整个表或基于一个参数的结果的例子,我不知道如何做那个简单的查询。
这似乎是一个简单的任务,我将感谢任何帮助,因为我花了5个小时试图弄清楚这个问题。
这里是我的代码的一些关键组件,希望能帮助别人帮助我。我真诚地感谢你的帮助。控制器:
public class SignBuilderController : Controller
{
SignBuilderModel signBuilderModel = new SignBuilderModel();
//
// Initialize database entities
SignBuilderEntities _db;
public SignBuilderController()
{
_db = new SignBuilderEntities();
}
//
// Get /SignBuilder/PrintSetup
[Authorize]
public ActionResult PrintSetup()
{
var pricesList = signBuilderModel.GetPricingList().ToList();
return View("PrintSetup", pricesList);
}
// get Column1 and column 2 query
public ActionResult TestPage(int column1, int column1)
{
//do something here
return View();
}
在我的视图中,我将从输入字段中检索where子句的值。例子:
<input type="text" name="column1value" id="column1value" />
<input type="text" name="column2value" id="column2value" />
显然我也在使用一个模型,所以如果需要使这个工作没有问题。我真的在寻找样本代码,我可以作为一个例子来学习。我真的很感谢你的帮助,我要拔我的头发了。
试试这个(我假设你的视图是强类型的):
public ActionResult(int column1, int column2)
{
//do something here.
var model =
(
from p in this.GetPricingList()
where (p.column1 == column1) && (p.column2 == column2)
select p
).FirstOrDefault();
return View(model);
}
和View中的
<input type="text" name="column1" id="column1value" value="<%=Model.column1%>"/>
<input type="text" name="column2" id="column2value" value="<%=Model.column2%>"/>
您需要将值发送到控制器的Action方法
在视图中使用如下代码:
<% using (Html.BeginForm("RunQuery", "ControllerName")) { %>
<input type="text" name="column1" id="column1value" />
<input type="text" name="column2" id="column2value" />
<input type="submit" value="Run Query" />
<% } %>
和相应的动作方法
[HttpPost]
public ActionResult RunQuery(string column1, string column2)
{
var results = GetDataFromDatabase(column1, column2);
return View(results );
}
恐怕你的代码无法编译。你注释为控制器的东西根本没有从控制器类继承,你的actionresult方法也没有名字。我想知道你是如何在浏览器中访问你的页面的。代码应该像
public class MyApplicationModel:Controller
{
//use entity framework
private MyApplicationEntities entities = new MyApplicationEntities();
//
// Method to query database for price lists table
public IQueryable<pricingUploaded> GetPricingList()
{
return entities.pricingUploadeds;
}
//
// Method to query Column1 and Column2 in table pricingUploaded
[HttpGet]
public ActionResult Query()
{
//this action will render the page the first time
return View();
}
[HttpPost]//this method will only accept posted requests
public ActionResult Query(int column1, int column2)
{
//do something here.
var _result = entities.Where(x=>x.column1 == column1 && x.column2 == column2);
return View(_result);//pass result to strongly typed view
}
在你的视图中,你必须创建一个表单,当提交时可以将值发送到HttpPost重载的查询方法
<%:Html.BeginForm();%>
<input type="text" name="column1">
<input type="text" name="column2">
<input type="submit" id="save" value="query">
<%:Html.EndForm();%>
现在您在表单中输入值并单击查询按钮,您将在query Actionresult中获得column1和column2值,以接受发布的请求。你最好在那里设置一个断点,检查事情是如何真正工作的,并弄清楚如何做你需要做的事情。
Edit:基于你想要以ajax方式传递值的评论,你可以做一点jQuery:
<script type="text/javascript">
$(function(){
$("form").submit(function(){
$.ajax({
type:"post",
url:"myapplicationmodel/query",
success:function(data)
{
//do something with returned data
}
});
return false;// to prevent normal form post
});
});
</script>
这个呢:
public ActionResult(int column1, int column2)
{
var query = from m in _db.MyTable
where m.Column1 == column1
where m.Column2 == column2
select m;
// Or this for typed model
// select new MyModel { Column1 = m.Column1, etc }
var model = query.First();
return View(model);
}
我假设实体是Linq to Entity
对吗?如果是这样的话,你能不能用:
var model = from e in entities
where column1=16 and column2=24
select e;
,然后将该模型传递给视图。
基本上,如果entities
的类型为IQueryable
或IEnumerable
或任何其他可链接的接口,则可以执行该类型的查询。确保你的代码文件的顶部是using System.Linq;
。
希望有帮助!