使用BeginForm()将对象传递给控制器 [英] Passing an Object to a controller using BeginForm()

查看:99
本文介绍了使用BeginForm()将对象传递给控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个强类型的视图,我试图在单击按钮时将来自文本框的输入传递给使用BeginForm的操作.我的代码不断将null对象传递给控制器​​中的action方法.如何通过表单将对象传递给控制器​​?

I have a strongly typed view and I'm trying to pass the input from a textbox upon a button click to an action using BeginForm. My code keeps passing a null object to the action method in the controller. How do I pass the object to the controller via the form ?

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

推荐答案

我认为您已经很接近了,但请进行这些更改&它应该可以按预期工作:

I think you're fairly close, but make these changes & it should work as expected:

型号:

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;
...

这篇关于使用BeginForm()将对象传递给控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆