English 中文(简体)
MVC3 * 创建处理多种类型的视图
原标题:MVC3 :: Create a view to handle multiple types

我有一个情况, 我想要从控制器中接受一个“ 添加视图” int, 但返回一个不同的类型到 HttpPPPost 控制器方法。 配置, 我知道。 添加视图用于创建一个被输入为部件的对象, 但我需要通过部件目录的 ID 。 因此在我的部件控制器中, 我有一个方法, 比如 :

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

我的问题是,如果要返回部件,我如何将元件类别ID 输入到控制器中? 我希望在窗体中添加一个隐藏的字段, 像这样 :

@Html.Hidden(id)

如有任何想法,将不胜感激。

谢谢!

最佳回答

以名称检查请求参数和尝试设置模型属性。如果需要更高级的东西,您可以查看自定义模型绑定。Btw,您可以将动作修改为:

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

I slightly prefer the second form f或creations, but I guess it s a matter of tastes

Btw, your view s form shall have inputs f或each "important" property of the Widget object. (Hidden 或text) via:

@Html.HiddenF或(m => m.Id);
问题回答

假设您的查看模式 元件 元件 属性 元件CategoryId 属性

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 View > 中,使用 Hiddenfor HTML 助手方法,在隐藏变量中保留该变量。

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

当您提交时, 现在您将会在您的 < code> HTTPPost 动作方法中找到它 。

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

在您的视图代码中,您不仅指定视图将 return a Widget type, 而且还指定该视图的整个模式是 Widget type。 基本上,数据通过 < em> into ,通过 model 的视图,其类型为 Widget

您可以在这里做的是保留“ 视图” 的强写到 < code> 元件 , 但是如果您需要简单地通过一个 ID 值( 简单的 < code> int ), 您可以使用 < href=" http:// msdn. microsoft.com/ en- us/ library/ system.web. mvc. controlbag% 28v=vs. 98. 29. aspx" rel=" nofolpolation.web. mvc.viewdata. viewdata. aspx" rel=" nofolvection" > ViewData

例如,在控制器中:

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)
}

请注意, ViewData >>ViewBag ,通过这个机制,“非模版”数据可以传递到视图中。 ViewBag是较新的(MVC 3),并且以 < a href=" http://msdn.microsoft.com/en-us/library/dd264741.aspx" rel=“nofolpolt"\\code C#4.0的特性为基础,而ViewData是基于收集密钥/价值对的旧方法。

在主计长的添加动作中, 您可以使用 ViewBag 属性在 id 和 View do html. Hidden () 中设置 ViewBag 属性。 I. e.

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)




相关问题
Howto get started with C# 4.0 and .NET 4.0?

I don t want to download Visual Studio 2010. How can I start studying (not developing real applications) C# 4.0 and .NET 4.0 with just a text editor? Can I just download C# 4.0 compiler and .NET 4.0 ...

Mocking Framework with C# 4.0 Support?

Anybody know of a mocking framework that supports C# 4.0? Doesn t matter which one ATM, just need something that will work.

Unit Testing interface contracts in C#

Using the Code Contracts tools available in VS2010 Beta 2, I have defined an interface, a contract class for that interface and two classes that implement the interface. Now when I come to test the ...

How to Iterate Through Array in C# Across Multiple Calls

We have an application where we need to de-serialize some data from one stream into multiple objects. The Data array represents a number of messages of variable length packed together. There are no ...

IronPython ScriptRuntime equivalent to CPython PYTHONPATH

The following import works inside ipy.exe prompt but fails using IronPython ScriptRuntime inside a C# 4.0 program. import ConfigParser C# code: using System; using System.Collections.Generic; using ...

i cant understand the following code

Matrix<float> trainData2 = trainData.GetRows(intVar >> 1, intVar, 1); intVar is integer type... please help me to understand this code.