asp.net-mvc-3 - 在 ActionResult 中传递多个值

标签 asp.net-mvc-3 razor

我在 mvc 3 razor 中有这段代码

@using (Html.BeginForm("MyAction", "MyController"))
{
    <input type="text" id="txt" name="txt"/>          
    <input type="image" src="image.gif" alt="" />
}   

在 Controller 中我有这个代码

[HttpPost]
public ActionResult MyAction(string text)
{
    //TODO something with text and return value...
}

现在,如何发送新值,例如 id 到 Action 结果???谢谢

最佳答案

您使用 View 模型:

public class MyViewModel
{
    public string Text { get; set; }

    // some other properties that you want to work with in your view ...
}

然后将此 View 模型传递给 View :

public ActionResult MyAction()
{
    var model = new MyViewModel();
    model.Text = "foo bar";
    return View(model);
}

[HttpPost]
public ActionResult MyAction(MyViewModel model)
{
    // remove the Text property from the ModelState if you intend
    // to modify it in the POST controller action or HTML helpers will
    // use the old value
    ModelState.Remove("Text");
    model.Text = "some new value";
    return View(model);
}

然后 View 被强类型化到这个模型:

@model MyViewModel

@using (Html.BeginForm("MyAction", "MyController"))
{
    @Html.EditorFor(x => x.Text)
    <input type="image" src="image.gif" alt="" />
}

关于asp.net-mvc-3 - 在 ActionResult 中传递多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11220829/

相关文章:

c# - 使用 MVC3 创建和使用 WCF

c# - 没有在服务中实现接口(interface)成员

asp.net - 实际上如何在 Entity Framework 4 Code-First CTP 5 中执行关系?

asp.net-mvc-3 - MVC WebGrid-如何以编程方式获取当前页面,排序列等

javascript - 三个按钮从 Controller 调用相同的操作方法 - 确定调用哪一个方法

c# - 为什么这段代码会给我来自 Request.Form 的无效内容类型? (ASP.NET 核心)

asp.net-mvc-3 - ASP.NET MVC 3 Video.Flash助手不起作用

asp.net-mvc-3 - 使用 ASP MVC TempData 向用户显示消息时出现问题

c# - MVC3 [Required] 验证消息显示在表单提交之前

jquery - Html.ValidationMessageFor 带有图像和工具提示