2012-05-22 65 views
0

我有一种情况,我想'添加'视图从控制器接受一个int,但返回一个不同的类型到HttpPost控制器方法。令人困惑,我知道。 'Add'视图用于创建一个类型为Widget的对象,但我需要传入WidgetCategory的ID。所以在我的WidgetController,我想有一个方法是这样的:MVC3 ::创建一个视图来处理多种类型

public ActionResult Add(int id) // 'id' is the WidgetCategoryID 
{ 
    return View(id); 
} 

然而,在视图中,因为它打算返回要添加的窗口小部件应该像这样开头:

@using MyProject.Models 
@model Widget 
@{ 
    ViewBag.Title = "My Title"; 
    Layout = "~/Views/Shared/_MyLayout.cshtml"; 
} 
@using (Html.BeginForm()) 
{ 
    // Insert markup here ... 
} 

我问题是,如果输入WidgetCategoryID以返回Widget,如何将WidgetCategoryID传递给控制器​​?我希望将它作为隐藏字段添加到表单中,如下所示:

@Html.Hidden(id) 

任何想法都将不胜感激。

谢谢!

+0

我建议将参数名称从'id'更改为'widgetCategoryId'。如果你需要一个评论来告诉我这个变量代表了什么,你没有把它命名得很好。 –

+0

事实上,大部分答案假设你在'Add(int id)'上的'id'参数是用于'Widget.Id'而不是'Widget.WidgetCategoryId',这证明了我之前的观点,你需要重新命名它,而不是依赖在评论。 –

回答

1

默认模型绑定器按名称检查请求参数,并尝试根据模型设置模型属性。如果你需要更多的变化,你可以看看自定义模型绑定。顺便说一句,你可以改变你的行动:

public ActionResult Add(Widget widget) // Widget class has one property named Id with public set 
{ 
    return View(widget); 
} 

public ActionResult Add(int id) // 'id' is the WidgetCategoryID 
{ 
    Widget widget = new Widget(); 
    widget.Id = id; 
    return View(widget); 
} 

我稍微更喜欢创作的第二种形式,但我想它的口味

的问题

顺便说一句,您的视图的形式应该为Widget对象的每个“重要”属性提供输入。 (隐藏或文本)通过:

@Html.HiddenFor (m => m.Id); 
0

在你可以设置一个ViewBag属性id和它们在视图控制器的添加操作使用ViewBag财产做html.Hidden()。即,

public ActionResult Add(int id) // 'id' is the WidgetCategoryID 
{  
    Widget widget = new Widget();  
    widget.Id = id;  
    ViewBag.categoryId = id; 
    return View(widget); 
} 

在查看

@Html.Hidden(@:ViewBag.categoryId) 
2

假设你的视图模型WidgetWidgetCategoryId财产

public class Widget 
{ 
    public int ID { set;get;} 
    public int WidgetCategoryId { set;get;} 
    //Other properties 
} 

发送该给Add视图(HTTPGET)

public ActionResult Add(int id) 
{ 
    Widget objModel=new Widget{ WidgetCategoryId =id} ; 
    return View(objModel); 
} 

现在在您的Add视图中,使用HiddenForHTML帮助器方法将其保留在隐藏变量中。

@using MyProject.Models 
@model Widget 
@{ 
    ViewBag.Title = "My Title"; 
} 
@using (Html.BeginForm()) 
{ 
    @Html.HiddenFor(m=>m.WidgetCategoryId); 
    <input type="submit" value="Save" /> 
} 

现在你将有你的HTTPPost操作方法,当你提交。

[HttpPost] 
public ActionResult Add(Widget model) 
{ 
    // check model.WidgetCategoryId and Have fun with it 
} 
1

在您的视图代码中,你不仅指定视图将一个Widget类型,而是指定该视图的整个模型是一个Widget类型。基本上,该数据通过转换为,其中的View通过其@model的类型为Widget

什么,你可以在这里做是为了查看的强类型保留一个Widget,但如果你需要传递的只是一个ID值(一个简单的int),您可以使用该ViewDataViewBag

例如,在控制器:

public ActionResult Add(int id) // 'id' is the WidgetCategoryID 
{ 
    // All properties of a ViewBag are completely dynamic! 
    ViewBag.WidgetID = id; 

    // You're still returning a View strongly-typed to a Widget, but not 
    // actually supplying a Widget instance. 
    return View(); 
} 

并在视图:

@using MyProject.Models 
@model Widget 
@{ 
    ViewBag.Title = "My Title"; 
    Layout = "~/Views/Shared/_MyLayout.cshtml"; 
} 
@using (Html.BeginForm()) 
{ 
    // Retrieve the WidgetID from the ViewBag 
    var WidgetID = ViewBag.WidgetID; 

    // Do something with the WidgetID, for example: 
    @Html.Hidden(WidgetID) 
} 

请注意,ViewDataViewBagvery similar mechanisms,通过它可以将“非模型”数据传递到视图中。 ViewBag更新(MVC 3),基于C#4.0的dynamic功能,而ViewData是基于键/值对集合的较早方法。

相关问题