2014-02-10 185 views
0

我開發了一個自定義的HtmlHelper擴展方法,但該數據不是 發佈操作。如何在asp.net mvc中將數據從視圖發送到控制器操作?

的HtmlHelper擴展類:

public static class TestHtmlHelper 
{ 
    public static MvcHtmlString CreateControl(this HtmlHelper helper, string tagName, IDictionary<string, string> attributes) 
    { 
     var newTag = new TagBuilder(tagName); 
     newTag.MergeAttributes(attributes, true); 

     return MvcHtmlString.Create(newTag.ToString(TagRenderMode.Normal)); 
    } 

    public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes) 
    { 
     // Create tag builder 
     var builder = new TagBuilder("img"); 

     // Create valid id 
     builder.GenerateId(id); 

     // Add attributes 
     builder.MergeAttribute("src", url); 
     builder.MergeAttribute("alt", alternateText); 
     builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); 

     // Render tag 
     return builder.ToString(TagRenderMode.Normal); 
    } 

} 

//查看代碼

@using (Html.BeginForm("Go","Home",FormMethod.Post)) 
{ 
    IDictionary<string, string> d = new Dictionary<string, string>(); 
    d.Add("type", "text"); 
    d.Add("id", "text1"); 
    d.Add("required", "required"); 
    @Html.Raw(Html.CreateControl("input", d)) 
    @Html.Raw(Html.Image("image1", "/Images/bullet.png", "bullet", new { border = "4px" })) 
    d = null; 
    d = new Dictionary<string, string>(); 
    d.Add("type", "submit"); 
    d.Add("value", "Go"); 
    @Html.Raw(Html.CreateControl("input", d)) 
    <span></span> 
    d = null; 
    d = new Dictionary<string, string>(); 
    d.Add("value", "text"); 
    d.Add("id", "span1"); 
    d.Add("text", "required"); 
    @Html.Raw(Html.CreateControl("span", d)) 
} 

//控制器代碼

public ActionResult Index() 
{ 
    ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application."; 

    return View(); 
} 
[HttpPost] 
public ActionResult Go(string test) 
{ 
    return Content(test); 
} 

我沒有在字符串測試中獲取數據。我想將這些數據提交給數據庫。

+1

我看不出有任何輸入鍵入name = test在你的表單中。要獲取操作參數的值,這些輸入必須具有相同的名稱。 – ramiramilu

+2

選中此http:// stackoverflow。com/questions/18873098/html-asp-net -mvc-4-razor-get-textbox-input-value/18873263#18873263 – Andrei

回答

2

要獲取輸入值作爲MVC操作的參數,您需要爲輸入類型包含名稱

  • 我看不到名稱代碼中的任何輸入類型。
  • 我也沒有看到TEST在你的代碼

例如,如果您的形式是 -

@using (Html.BeginForm("Submit","Ajax",FormMethod.Post)) 
{ 
    <input type="text" name="Rami"/> 
    <input type="submit" value="Go"/> 
} 

輸出截圖 -

enter image description here

+4

這是一個奇怪的截圖。我以爲他們在SO裏面有VS調試器:-) –

+0

@MalcolmFrexner,hahaha :-)。更新了一個標題。 – ramiramilu

1

把你的輸入在表單標籤內。所有輸入數據將在表單提交中發送給控制器。請看例子:

查看

@using (Html.BeginForm("Search", "Events")) 
{ 
    @Html.TextBox("name") 
    <input type="submit" value="Search" /> 
} 

控制器

public class EventsController: Controller 
{ 
    public ActionResult Search(string name) 
    { 
     //some operations goes here 

     return View(); //return some view to the user 
    } 
} 

如果您需要更復雜的類型只是lern如何在ASP.NET使用模型來工作MVC。這裏是短例如:

剃刀

@model UserModel 

@using (Html.BeginForm("Search", "Events")) 
{ 
    @Html.TextBoxFor(m => m.FirstName) 
    @Html.TextBoxFor(m => m.LastName) 
    <input type="submit" value="Search" /> 
} 

控制器

public class EventsController: Controller 
{ 
    public ActionResult Search(UserModel model) 
    { 
     //some operations goes here 

     return View(); //return some view to the user 
    } 
} 

模型(C#)

public class UserModel 
{ 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
} 
相關問題