开发者

How do we represent the following piece of view code in a Spark view?

<% using (Html.BeginForm("AddToCart", "Cart")) { %>        
    <%= Html.Hidden("ProductID", pr.ProductID) %>
    <%= Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery) %>
    <input type="submit" value="+ Add to car开发者_如何学运维t" />
 <% } %> 

Currently I am using

# using (Html.BeginForm("AddToCart", "Cart")) {
    ${Html.Hidden("ProductID", pr.ProductID)}
    ${Html.Hidden("returnUrl", ViewContext.HttpContext.Request.Url.PathAndQuery)}
    <input type="submit" value="+ Add to cart" />
 #} 

Is this the right way? any better approach?


There is a much better way now with the latest version of Spark (v1.5) using the new Bindings feature. You can read my blog post on it here which has an example of Html Form for you.

Basically your html form code in your Spark view ends up using the MVC Html Form Helper properly, but looking awesome like this:

<Form class="form-default">
    <ValidationSummary Message="Login was unsuccessful.
    Please correct the errors and try again." ExcludePropertyErrors="true" />
    <div class="editor-label">
         <Label For="UserName" />
    </div>
    <div class="editor-field">
         <TextBox For="UserName" /><ValidationMessage For="UserName"/>               
    </div>
    <div class="editor-label">
         <Label For="Password" />
    </div>
    <div class="editor-field">
         <Password For="Password" /><ValidationMessage For="Password" />
    </div>
    <div class="editor-label">
        <CheckBox For="RememberMe" />
        <Label For="RememberMe" />
    </div>
    <input type="submit" value="Log On" />
</Form>

You can also see a code sample project that uses it in the Spark code base here.

Hope that helps,
Rob


I assume you are referring to how the form tags are handled. You can do this:

#Html.BeginForm("AddToCart", "Cart");
...
#Html.EndForm();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜