开发者

How do I access my post data in a asp.net mvc editor view?

I have gotten this semi autogenerated code, but I am uncertain where the Post data is saved and how I access the variables in my controller so I can validate and upload it to my database.

    @model FirstWeb.Models.Picture

@{
    ViewBag.Title = "Upload et billede";

}

<开发者_如何学C;h2>Upload et billede</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>



@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

        <input type="file" name="file" id="file" />

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ConcertYear)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConcertYear)
            @Html.ValidationMessageFor(model => model.ConcertYear)
        </div>

        <p>
            <input type="submit" value="Upload" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Tilbage til billeder", "Index")
</div>


It seems that you are trying to upload files here. Checkout the following blog post. You will need to use a multipart/form-data enctype for your form in order to be able to upload files. So the first step is to fix your form definition:

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })) 
{
    ...
}

then update your view model so that it takes the uploaded file as property:

public class Picture
{
    public HttpPostedFileBase File { get; set; }
    public string Title { get; set; }
    public int ConcertYear { get; set; }

    ... some other properties used in the view
}

and and finally have your controller POST action take this view model as parameter:

[HttpPost]
public ActionResult Foo(Picture model)
{
    if (!ModelState.IsValid)
    {
        // there were validation errors => re-display the view
        return View(model);
    }

    // the model is valid at this stage => check if the user uploaded a file
    if (model.File != null && model.File.ContentLength > 0)
    {
        // the user uploaded a file => process it ...
    }
    ...
} 


The Edit action should take your model as a parameter.

Its proeprties will have the edited values.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜