如何将数据从视图发送到asp.net mvc中的控制器操作?

时间:2014-02-10 12:04:15

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 razor

我开发了一个自定义的HtmlHelper扩展方法,但该数据不是 张贴行动。

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

我没有在字符串测试中获取数据。我想将这些数据提交给DB。

2 个答案:

答案 0 :(得分:2)

要将输入值作为MVC操作的参数,您需要为输入类型包含 NAME

  • 我没有看到代码中任何输入类型的 NAME
  • 此外,我在您的代码中未看到 TEST

例如,如果您的表单是 -

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

输出ScreenShot -

enter image description here

答案 1 :(得分: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
   }
}

如果您需要使用更复杂的类型,请了解如何在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; }
}