Skip to main content
 首页 » 编程设计

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

2024年02月06日21lori

我有一个强类型 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"/> 
} 

这是 Action 方法:

[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; 
     //... 
}