超级简单视图引擎:@Each无法渲染

时间:2014-06-14 07:36:09

标签: nancy

为了处理书籍创建后请求,我有一个像下面这样的处理程序:

Post ["/edit"] = parameters => {
            var book = this.Bind<Book>();
            book.Id = new ObjectId(this.Request.Form["Id"]);
            if(book.Id == null){
                return Response.AsRedirect("/books");
            }
            var result = this.Validate(book);
            if(result.IsValid){
                bookRepository.Update(book);
                return Response.AsRedirect("/books");
            }else{  
                dynamic model = new DynamicDictionary();
                model.Book = book;

                var  errors = new List<string>();
                foreach(var key in result.Errors.Keys){
                    foreach(var value in result.Errors[key]){
                        errors.Add(string.Format("{0} - {1}", key, value));
                    }
                }

                model.ErrorMessages = errors;
                return View ["book_edit.html", model];
            }
        };

还有book_edit.html:

<html>
<head>
    <title>Edit book</title>
</head>
<body>
    <h2>Edit book</h2>
    <p>
        <a href="/books">Book listing</a>
    </p>
    <h4>Validation Errors</h4>
    @Partial['book_edit_error.html', Model.ErrorMessages];
    <ul style="color:red">
        @Each.ErrorMessages
            <li>@Current</li>
        @EndEach
    </ul>
    <form method="POST" action="/books/edit">
        <table>
            <tr>
                <td>Name: </td>
                <td>
                    <input type="text" name="Name" value="@Model.Book.Name"/>
                    <input type="hidden" name="Id" value="@Model.Book.Id"/>
                </td>
            </tr>
            <tr>
                <td>Author: </td>
                <td><input type="text" name="Author" value="@Model.Book.Author"/></td>
            </tr>
            <tr>
                <td>Description: </td>
                <td><input type="text" name="Description" value="@Model.Book.Description"/></td>
            </tr>
            <tr>
                <td><input type="submit" value="Save"/></td>
            </tr>
        </table>    
    </form>
</body>

@Each语句按预期工作: enter image description here

我尝试了很多次,但即使我尝试使用@Partial,它也根本无法工作。

但是当使用@Each和List模型时(不是@ Each.Properties,它可以工作。

1 个答案:

答案 0 :(得分:0)

尝试 DynamicDictionary

dynamic model = new DynamicDictionary();

更改为 ExpandoObject

dynamic model = new ExpandoObject();

也许工作,祝你好运!