2010-03-29 106 views
1

我有一個觀點,即呈現這樣的:最佳策略值

alt text

「項目1」和「項目2」是從表<tr>元素。

用戶改變「值1」或「值2」之後,我想打電話給一個控制器,並把結果(一些HTML片段)中標記爲「結果的...」的div

我有一些模糊的JQuery概念。例如,我知道如何綁定Select元素的onchange事件,並調用$.ajax()函數。

但我不知道這是否可以在ASP.NET MVC2中以更有效的方式實現。

回答

0

這是我取得了巨大成功使用的方法的例子:

在查看:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Namespace.Stuff>>" %> 

<asp:Content ID="Content3" ContentPlaceHolderID="head" runat="server"> 
    <script type="text/javascript"> 
    $(document).ready(function(){ 
     $("#optionsForm").submit(function() { 
      $("#loading").dialog('open'); 
      $.ajax({ 
       type: $("#optionsForm").attr("method"), 
       url: $("#optionsForm").attr("action"), 
       data: $("#optionsForm").serialize(), 
       success: function(data, textStatus, XMLHttpRequest) { 
        $("#reports").html(data); //replace the reports html. 
        $("#loading").dialog('close'); //hide loading dialog. 
       }, 
       error: function(XMLHttpRequest, textStatus, errorThrown) { 
        $("#loading").dialog('close'); //hide loading dialog. 
        alert("Yikers! The AJAX form post didn't quite go as planned..."); 
       } 
      }); 
      return false; //prevent default form action 
     }); 
    }); 
    </script> 
</asp:Content> 

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> 

    <div id="someContent"> 
     <% using (Html.BeginForm("Index", "Reports", FormMethod.Post, new{ id = "optionsForm" })) 
      { %> 

      <fieldset class="fieldSet"> 
      <legend>Date Range</legend> 
      From: <input type="text" id="startDate" name="startDate" value="<%=ViewData["StartDate"] %>" /> 
      To: <input type="text" id="endDate" name="endDate" value="<%=ViewData["EndDate"] %>" /> 
      <input type="submit" value="submit" /> 
      </fieldset> 

     <%} %> 
    </div> 

    <div id="reports"> 
     <%Html.RenderPartial("ajaxStuff", ViewData.Model); %> 
    </div> 

    <div id="loading" title="Loading..." ></div> 
</asp:Content> 

在控制器:

public ActionResult Index(string startDate, string endDate) 
{ 
    var returnData = DoSomeStuff(); 

    if (Request.IsAjaxRequest()) return View("ajaxStuff", returnData); 
    return View(returnData); 
} 

上面的代碼概括了基本策略。你當然想要調整它的多個部分和多種形式。

+0

謝謝 - 它幫助! – pvieira 2010-03-30 08:37:40

+0

很高興能幫到你! – 2010-03-30 15:00:29