【发布时间】:2013-09-23 14:43:52
【问题描述】:
我在构建查询字符串时遇到了一些问题。我有一个模型:
[HttpGet]
public partial class Task
{
public System.Guid UniqueID { get; set; }
public string Description { get; set; }
public decimal Priority { get; set; }
public long TaskTypeId { get; set; }
public TaskStatus TaskStatus { get; set; }
public string GroupWorkspaceUrl { get; set; }
public Nullable<System.DateTime> StartDate { get; set; }
public Nullable<System.DateTime> Deadline { get; set; }
public Nullable<int> PlannedHours { get; set; }
{...}
}
我在控制器中有 2 个动作:
[HttpGet]
public virtual ActionResult TaskCreate(string schemaType)
{
var model = new Task();
model.Accept(_taskService.ReaderVisitor, schemaType)
{...}
}
[HttpGet]
public virtual ActionResult TaskCreateWithModel(string schemaType, Task model)
{
SetDefaultValues(model);
model.Accept(_taskService.ReaderVisitor, schemaType);
{...}
}
我想从其他 C# WinForms 解决方案构建一个查询字符串,该解决方案调用控制器中的第二个操作(public virtual ActionResult TaskCreateWithModel(string schemaType, Task model)),但我不知道如何发送任务模型在查询字符串中?我试图调用这个查询字符串:http://localhost:82/Task/TaskCreate?schemaType=default&Description=someDesciption,但总是调用第一个操作。如何使用任务模型构建查询字符串?
【问题讨论】:
-
你是什么意思:“但总是第一个动作是调用”?
-
您不能将
object(在本例中为Task)作为参数传递给HttpGet操作。此外,您的查询字符串显示TaskCreate,如果您想调用第二种方法,那么它应该是TaskCreateWithModel -
localhost:82/Task/… - 查询字符串总是调用第一个(public virtual ActionResult TaskCreate(string schemaType)) 动作
-
更改为 POST 将允许您在请求正文中传递对象,并且无论如何都是“创建”操作的正确/标准/正确/最佳方法
-
使用具有相同控制器动作名称的 [HttpPost] 以使该动作在带有查询字符串的表单提交时成为可能
标签: c# asp.net-mvc asp.net-mvc-4