我開發了一個自定義的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);
}
我沒有在字符串測試中獲取數據。我想將這些數據提交給數據庫。
我看不出有任何輸入鍵入name = test在你的表單中。要獲取操作參數的值,這些輸入必須具有相同的名稱。 – ramiramilu
選中此http:// stackoverflow。com/questions/18873098/html-asp-net -mvc-4-razor-get-textbox-input-value/18873263#18873263 – Andrei