2012-09-24 12 views
0

我有一個網頁表單,要求用戶輸入他們的ID。我的總體目標是能夠接受id並調用一個函數並返回一個類。該類將返回用戶名,電子郵件和城市。 我還沒有那麼遠。我仍然在第一階段。如何獲得用戶輸入以在我的mvc控制器中創建特定方法?

我的html正確嗎?當按鈕被擊中時,我希望用戶輸入通過控制器?

我想從html中的輸入文本中獲取文本。我想從我的html表單中接收輸入文本。然後採取文本到調用用戶類的控制器和顯示類

<html> 
     <div align="center"> 
     <form id="searchUser" method="post" action=""> 
      <table align="center"> 
     <tr> 
      <td class="label"> 
      Enter ID: 
      </td> 
      <td> 
       <input type="text" name="UserId" id="UserId" /> 
      </td> 
     </tr> 
     <tr> 
      <td> 
       <button class="searchButton" id="searchButtong">Search</button> 
      </td> 
     </tr> 
     </table> 
    </form> 
    </div> 
    <hr /> 
</html> 


Search Controller 

public class SearchController : Controller 
{ 
    // 
    // GET: /Search/ 

    public ActionResult Index() 
    { 
     return View(); 
    } 

    public string searchUser(string UserId) 
    { 
     UserId = Request["UserId"]; 
     return UserId; 
    } 
} 

回答

1

最簡單的方法很可能會做出一個Ajax請求的動作,並有行動JSON返回正確的數據。

public JsonResult YourActionName(int id) 
{ 
    // Fetch your data from DB, or create the model 
    // in whatever way suitable 
    var model = new YourModel { 
     UserName = "Something" 
    }; 

    // Format the response as JSON 
    return Json(model); 
} 
+0

進入AJAX之前,我認爲這將是更好地讓OP熟悉自己用一些基本的HTTP協議原則,他目前丟失了恕我直言。 –

+0

@DarinDimitrov同意,再次通讀問題後,我意識到OP沒有提到他想通過AJAX來做到這一點。不知道我從哪弄來的,肯定是那些讓我困惑的純HTML。 –

+0

抱歉Christofer – Yusuf

2

您已經在控制器操作中聲明瞭UserId變量。你並不需要再次從Request取,

public class SearchController : Controller 
{ 
    public ActionResult Index() 
    { 
     return View(); 
    } 

    [HttpPost]  
    public ActionResult SearchUser(string UserId) 
    { 
     User user = ... go and fetch the user given the user id 
     // from wherever your users are stored (a database or something) 
     return View(user); 
    } 
} 

現在你可以把你的觀點強類型的User類,並有顯示結果的部分:

<%@ Page 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.User>" 
%> 
<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Demo</title> 
</head> 
<body> 
    <div align="center"> 
     <% using (Html.BeginForm("SearchUser", "Search")) { %> 
      <table align="center"> 
       <tr> 
        <td class="label"> 
         Enter ID: 
        </td> 
        <td> 
         <input type="text" name="UserId" id="UserId" /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <button class="searchButton" id="searchButtong">Search</button> 
        </td> 
       </tr> 
      </table> 
     <% } %> 
    </div> 

    <hr /> 

    <% if (Model != null) { %> 
     <!-- Display the User results here --> 
     <div> 
      <%= Html.DisplayFor(x => x.FirstName) %> 
     </div> 
     <div> 
      <%= Html.DisplayFor(x => x.LastName) %> 
     </div> 
    <% } %> 
</body> 
</html> 

現在您已經使用標準HTML技術實現了此功能,您可以通過引入AJAX來改進它:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8" /> 
    <title>Demo</title> 
</head> 
<body> 
    <div align="center"> 
     <% using (Ajax.BeginForm("SearchUser", "Search", null, new AjaxOptions { UpdateTargetId = "results" })) { %> 
      <table align="center"> 
       <tr> 
        <td class="label"> 
         Enter ID: 
        </td> 
        <td> 
         <input type="text" name="UserId" id="UserId" /> 
        </td> 
       </tr> 
       <tr> 
        <td> 
         <button class="searchButton" id="searchButtong">Search</button> 
        </td> 
       </tr> 
      </table> 
     <% } %> 
    </div> 

    <hr /> 

    <div id="result"></div> 

    <!-- TODO: Adjust with the proper version of jQuery that you are using --> 
    <script src="<%= Url.Content("~/Scripts/jquery-1.7.1.min.js") %>" type="text/javascript"></script> 
    <script src="<%= Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js") %>" type="text/javascript"></script> 
</body> 
</html> 

,然後讓你的控制器動作返回一個PartialView:

[HttpPost]  
public ActionResult SearchUser(string UserId) 
{ 
    User user = ... go and fetch the user given the user id 
    // from wherever your users are stored (a database or something) 
    return PartialView(user); 
} 

,你需要定義(~/Views/Search/SearchUser.ascx):

<%@ Control 
    Language="C#" 
    Inherits="System.Web.Mvc.ViewUserControl<mVCaPPLICATION1.mODELS.uSER>" 
%> 
<!-- Display the User results here --> 
<div> 
    <%= Html.DisplayFor(x => x.FirstName) %> 
</div> 
<div> 
    <%= Html.DisplayFor(x => x.LastName) %> 
</div> 
+0

我的html代碼是否正確?當按鈕被擊中時,我希望用戶輸入通過控制器? – Yusuf

+0

不,您的HTML不好,因爲您尚未指定要在窗體上調用的控制器操作。 –

+0

我如何指定contorller動作來調用窗體?你可以在aspx引擎中顯示我嗎? – Yusuf

1

你的表單標籤是決定在POST會去當一個用戶提交表單。您目前有:

<form id="searchUser" method="post" action=""> 

這將指示POST到當前地址。

您可以使用MVC形式幫手很容易地改變這一點:

@using (Html.BeginForm()) { 
    //Your form here 
} 

或者使用正確的URL,或URL幫助它,你可以手動曲柄。

網址助手

<form id="searchUser" method="post" action="@Url.Action("searchUser", "Search")"> 

手用彎

<form id="searchUser" method="post" action="/Search/searchUser/"> 
+0

有沒有辦法在aspx引擎中做到這一點? – Yusuf

+0

是的,對不起 - 我以爲剃刀。使用'<%using(Html.BeginForm()){%>'而不是ampersat語法。將參數傳遞給'BeginForm'以指向您的控制器和操作。 – Fenton

1
$(function(){ 
    $("#searchButtong").click(function(e){ 
    e.preventDefault(); 
    var usrId=$("#UserId").val(); 
    $.getJSON("@Url.Action("searchUser","search")/"+usrId,function(data){ 
     //here you may set this to a div in the dom instead of alerting. 
     alert(data.UserName); 
     alert(data.UserID); 
    }); 
    }); 
}); 

假設你已經有動作返回一個JSON這樣

public ActionResult searchUser(int id) 
{ 
    UserEntity user=repo.GetUserFromID(id); 
    return Json(new { UserName=user.UserName, ID=id}, 
               JsonRequestBehaviour.AllowGet); 
} 
+0

爲什麼我得到json文本?我只是從html中的輸入文本中獲取文本。我想從我的html表單中接收輸入文本。然後將該文本傳給控制器,調用用戶類並顯示該類 – Yusuf

+0

這就是代碼所做的。它讀取文本框的值並通過getJSON發送到行動methdo – Shyju

相關問題