views:

32

answers:

1

I'm studying ASP MVC, and developping SportsStore (Create/Edit feature). When Create a product, Create action will view a Edit view, but when press Sudmit, it call action Create (Post), althrough I will set it call Edit action:

<% using (Html.BeginForm("Edit", "Admin", FormMethod.Post, new { enctype="multipart/form-data" }))
   {%>
    <%--<%= Html.ValidationSummary() %>--%>
    <%--<%= Html.Hidden("ProductID") %>--%>

    <p>Name: <%= Html.TextBox("Name")%>
        <div><%= Html.ValidationMessage("Name")%></div>
    </p>
    <p>Description: <%= Html.TextArea("Description", null, 4, 20, null)%>
        <div><%= Html.ValidationMessage("Description")%></div>
    </p>
    <p>Price: <%= Html.TextBox("Price")%>
        <div><%= Html.ValidationMessage("Price")%></div>
    </p>
    <p>Category: <%= Html.TextBox("Category")%>
        <div><%= Html.ValidationMessage("Category")%></div>
    </p>
    <p>
        Image: 
        <% if (Model.ImageData == null)
           { %>
            None
        <% }
           else
           { %>
            <img src= "<%= Url.Action("GetImage", "Products", new {Model.ProductID}) %>" />
        <% } %>
        <div>Upload new image: <input type="file" name="file" id="file" /></div>
    </p>
    <input type="submit" value="Save" /> 
    <%= Html.ActionLink("Cancel and return to list", "Index")%>

<% } %>

Please help me fix it

A: 

The code you have seems reasonable if you want it to post back to the Edit action. Your question is a bit confusing, but I'm going to assume that you want to reuse the view and have it post back to Create when rendered from Create and Edit when rendered from Edit. The easiest way is to simply omit the parameters from the BeginForm call. This will cause the form action to be set to the current controller and action, which would give you what you seem to want. An alternative would be to develop templates (display/editor) for the model but have separate views for Create/Edit that simply render the template Html.EditorFor( m => m, "ProductTemplate" ). This would allow you to customize the view -- perhaps the Create view requires you to upload an image? -- yet still reuse most of the code.

tvanfosson
I will try as your comment.But I don't know why I set BeginForm method is have to call to action edit of controller Admin, but it call action which called it (action Create)
@quasar747 - Did you check the generated HTML to see what the actual form action is?
tvanfosson
When debug, I overmouse on Save button, I see IE's status is Create
@quasar747 -- just do a view source and check the action attribute of the form tag.
tvanfosson
Thanks to tvanfosson, I fixed this error. Error appear from file Admin.Master, but debugger did not notice.Error is that lines:<form id="form1" runat="server"></form>Sorry, My English is not good :)