c# - Upload Images while creating new Model -
i'm going create profile users in asp.net mvc application. users creation controller this:
[httppost] [validateantiforgerytoken] public actionresult create(userprofileviewmodel userviewmodel) { if (modelstate.isvalid) { .... } return view(userviewmodel); }
besides, each user model got several properties including 1 photo. goes till want add input field accept photo.
@html.textboxfor(model => model.imageurl, new { type = "file" })
and add below field userprofileviewmodel
:
[display(name = "profile photo")] public httppostedfilebase imageurl { get; set; }
among snippets upload photo , answers my previous question, seems uploading photo considered separate task. i.e. need individual form , controller upload photo (like first part of answer).
i want know there methods can create whole user profile in 1 form , pass photo same controller (which included photo in userprofileviewmodel
)?
i need note don't know use jquery or ajax , want use standard html helpers task.
update: view looks this:
@model projectmanager.models.userprofileviewmodel @{ viewbag.title = "create"; } <h2>@viewbag.title</h2> @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>user profile</h4> <hr /> @html.validationsummary(true) <div class="form-group"> @html.labelfor(model => model.description, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.description) @html.validationmessagefor(model => model.description) </div> </div> <div class="form-group"> @html.labelfor(model => model.name, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.name) @html.validationmessagefor(model => model.name) </div> </div> <div class="form-group"> @html.labelfor(model => model.age, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.age) @html.validationmessagefor(model => model.age) </div> </div> <div class="form-group"> @html.labelfor(model => model.imageurl, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.textboxfor(model => model.imageurl, new { type = "file" }) @html.validationmessagefor(model => model.imageurl) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="ثبت" class="btn btn-default" /> </div> </div> </div> } <div class="rtl text-right"> @html.actionlink("back list", "index") </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
file inputs not sent in request unless form element contains enctype = "multipart/form-data"
attribute. change view code to
@using (html.beginform("create", "user", formmethod.post, new { enctype = "multipart/form-data" })) { .... }
Comments
Post a Comment