开发者

ASP.NET MVC - Return dictionary parameters

Alright, I'm tryin开发者_如何转开发g to pass a Dictionary, where key = int, and value = Prototype into my view.

Prototype:

public class Prototype     
{
    public string Value { get; set; }
    public string PropertyName { get; set; }
    public Options Type { get; set; }
}

After the Dictionary has been passed into my view I'm doing a foreach loop to render each of the KeyValuePair:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<Dictionary<System.Int32,MvcApplication1.Models.Prototype>>" %>
<%@ Import Namespace="SecuredFormExample.Code" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>Index</title>
</head>
<body>
    <div>

    <% Html.BeginForm("Save", "Products", FormMethod.Post); %>

    <% foreach (KeyValuePair<int, Prototype> p in Model)
       { %>
       <%: Html.Label(p.Value.PropertyName) %>
       <%: Html.TextBox("Value") %>
       <% } %>
    <p><input type="submit" value="submit" /></p>

    <% Html.EndForm(); %>

    </div>
</body>
</html>

Now is the question, is it possible to pass the other values in the KeyValuePair value part back to the action aswell, or do I need to throw them all into hidden fields?


If you want to get the values back into the action you are posting to you need to send them along the request. Using hidden fields is one way of doing it. Another way is to only send an identifier which will allow you to fetch those values from a repository in the controller action you are posting to, the same way you are doing it in the controller action which renders the form.

If you are worried about users tampering with hidden fields you could serialize and encrypt entire objects using the Html.Serialize.


UPDATE:

Here's an example to expand on my second proposition which was to use an id:

<% Html.BeginForm("Save", "Products", FormMethod.Post) { %>
    <%: Html.Hidden("id", "PUT AN ID HERE WHICH IDENTIFIES THE ITEM")
    <% foreach (KeyValuePair<int, Prototype> p in Model) { %>
       <%: Html.Label(p.Value.PropertyName) %>
       <%: Html.TextBox("Value") %>
    <% } %>
    <p><input type="submit" value="submit" /></p>
<% } %>

and in your controller action:

public ActionResult Save(string id, ....) 
{
    var data = SomeRepository.FetchData(id);
    ...
}


Just seen your update, your options are limited only by HTML and Browser capabilities, so things like Hidden fields seem natural here. Other choices like scripting could be made to work but are way more complex than the automatic behaviour of hidden fields.

MVC Futures shows where the general path. It has a bunch of HtmlHelper extensions to generate hidden fields and encode object state into those fields. Of course you may have extra requirements like tamper-security etc. that you could layer on top of these methods.

http://aspnet.codeplex.com/SourceControl/changeset/view/55373#338518

  • BinaryHtmlExtensions.cs
  • ExpressionInputExtensions.cs

Effectively these'd be treated like old school ViewState, where you'd reconstitute model state in your controller by decoding these fields.


The options for passing non-visible values from one page to the next in ASP.NET MVC are:-

1) Hidden fields (encrypted or otherwise)

2) Cookies (hidden or otherwise)

3) Url parameters (encoded or otherwise, limited size)

4) Session state (stuffing this with big objects is generally not a good idea)

5) TempData (like session state but lasts just one 'cycle')

6) Database (pass just a key so you can retrieve the data on the next page)

For this example, if your data isn't too large and you aren't on a load balanced Web Farm, I'd recommend TempData as the easiest solution. (If you are on a load balanced farm you can still do it but you may need to change how session state is handled).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜