c# - 使用 BeginForm() 将对象传递给 Controller

标签 c# asp.net-mvc

我有一个强类型 View ,并且我尝试使用 BeginForm 将单击按钮时文本框中的输入传递到操作。我的代码不断将空对象传递给 Controller ​​中的操作方法。如何通过表单将对象传递给 Controller ​​?

@using (@Html.BeginForm("GetQueueInfoWorkorder","Home", FormMethod.Post, new { id = Model}))
{
    @Html.TextBoxFor(x=> x.ID);
    <input type="Submit" value ="Search" class="ui-button-icon-secondary"/>
}

这是操作方法:

[HttpPost]
public ActionResult GetQueueInfoWorkorder(UserResponse id)
{
    //Check queue complete
    int woNumber = Convert.ToInt32(id);
    tb_QueueCompleted completed = db.tb_QueueCompleted.SingleOrDefault(x => x.WorkOrderNumber == woNumber);
    if (completed != null)
    {
        var u = new UserResponse { ID = completed.QueueId.ToString() };
        GetLogInfoCompleted(u);
        return View("GetLogInfo");
    }

    //check queue pending

    return View();
}

最佳答案

我认为你已经相当接近了,但是进行这些更改并且它应该按预期工作:

型号:

public class UserResponse
{
    public int ID { get; set; }
}

查看:

@model UserResponse
@using (Html.BeginForm("GetQueueInfoWorkorder", "Home"))
{
    @Html.TextBoxFor(x => x.ID);
    <input type="Submit" value ="Search" class="ui-button-icon-secondary"/>
}

操作方法:

public ActionResult GetQueueInfoWorkorder(UserResponse model)
{
     int woNumber = model.ID;
     //...
}

关于c# - 使用 BeginForm() 将对象传递给 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24632528/

相关文章:

c# - 使用对象实例的具有动态字段(问题)和选项(答案)的 Bot 框架 FormFlow

c# - 如果另一条消息即将说出,如何停止 SpeakAsync?

javascript - ASP.NET MVC jQuery 不访问空模型对象

c# - MVC EF - System.Data.Entity.Infrastruct.DbUpdateException

c# - 重叠的矩形

c# - 如何将数据绑定(bind)到密封类?

c# - 在基本 View 模型中定义命令

c# - 混合组和用户的 asp.net mvc [Authorize()] 属性

c# - 在 MVC 4 Razor View 中引用外部 dll

c# - 将 datauri pdf 转换为文件以附加到电子邮件