C#MVC查询WHERE子句和View中的2个参数

时间:2011-04-12 14:45:29

标签: c# sql asp.net-mvc asp.net-mvc-3

我需要使用WHERE子句查询我的数据库,并将参数从我的视图传递给控制器​​。我完全失去了,因为我是一名JavaScript和PHP开发人员,目前正在学习dotnet MVC(我正在使用vs10 framework4)。

我本质上需要进行此查询:

select * from myTable where column1=16 and column2=24

column1和column2的值在我的View中。我已经找到了关于如何拉出整个表的一百万个例子,或者基于一个参数的结果,我无法弄清楚如何进行这种简单的查询。

这似乎是一项简单的任务,我会感激任何帮助,因为我花了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" />

显然我也在使用模型,所以如果需要这样做,这项工作没问题。我真的在寻找可以用作例子来学习的示例代码。我真的很感激任何帮助,我即将拔掉头发。

5 个答案:

答案 0 :(得分:6)

试试这个(我假设您的视图是强类型的):

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);     
} 

并在视图中:

<input type="text" name="column1" id="column1value"  value="<%=Model.column1%>"/> 
<input type="text" name="column2" id="column2value"  value="<%=Model.column2%>"/> 

答案 1 :(得分:2)

您需要将值发布到控制器上的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 ); 
}

答案 2 :(得分:2)

恐怕你不会编译。您注释为控制器的内容根本不会从控制器类继承而您的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值。你最好在那里设一个断点,检查事情是如何运作的,并弄清楚如何做你需要做的事情。 编辑:根据你想要传达值的评论,你可以做一点点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>

答案 3 :(得分:1)

我假设实体是Linq to Entity对吗?如果是这种情况,你不能使用:

var model = from e in entities
            where column1=16 and column2=24
            select e;

然后将该模型传递给vew。

基本上,如果entities属于IQueryableIEnumerable类型或任何其他可连接接口,则可以执行该类型的查询。确保您在代码文件的顶部using System.Linq;

希望有所帮助!

答案 4 :(得分:1)

这个怎么样:

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);
}