SianiD
دوشنبه 14 مرداد 1392, 18:04 عصر
با سلام خدمت دوستان و اساتید
من میخواستم یک تصویر رو آپلود کنم این کد ها رو ده بار استفاده کردم جواب داده اما اینبار نمیدونم چرا مقدار نال میفرسته واسه Action اینم کدهاش :
View :
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>افزودن تصویر</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ImageUrl)
</div>
<div class="editor-field">
<input type="file" name="imageUrl" id="imageUrl" />
@Html.ValidationMessageFor(model => model.ImageUrl)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Menu_Fk)
در این وب سایت فقط انتخاب زیر منو های گالری تصاویر مجاز است
</div>
<div class="editor-field">
@Html.DropDownList("Menu_Fk", String.Empty)
@Html.ValidationMessageFor(model => model.Menu_Fk)
</div>
<p>
<input type="submit" value="افزودن" />
</p>
</fieldset>
}
Controller::
public ActionResult Create(SwGallery swgallery, HttpPostedFileBase imageUrl)
{
if (ModelState.IsValid)
{
string newFilenameUrl = string.Empty;
if (imageUrl != null)
{
string filename = Path.GetFileName(imageUrl.FileName);
string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty)
+ Path.GetExtension(filename);
newFilenameUrl = "/Uploads/" + newFilename;
string physicalFilename = Server.MapPath(newFilenameUrl);
imageUrl.SaveAs(physicalFilename);
swgallery.ImageUrl = newFilenameUrl;
}
db.SwGalleries.Add(swgallery);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Menu_Fk = new SelectList(db.SwMenus, "Id", "Name", swgallery.Menu_Fk);
return View(swgallery);
}
من میخواستم یک تصویر رو آپلود کنم این کد ها رو ده بار استفاده کردم جواب داده اما اینبار نمیدونم چرا مقدار نال میفرسته واسه Action اینم کدهاش :
View :
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>افزودن تصویر</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ImageUrl)
</div>
<div class="editor-field">
<input type="file" name="imageUrl" id="imageUrl" />
@Html.ValidationMessageFor(model => model.ImageUrl)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Menu_Fk)
در این وب سایت فقط انتخاب زیر منو های گالری تصاویر مجاز است
</div>
<div class="editor-field">
@Html.DropDownList("Menu_Fk", String.Empty)
@Html.ValidationMessageFor(model => model.Menu_Fk)
</div>
<p>
<input type="submit" value="افزودن" />
</p>
</fieldset>
}
Controller::
public ActionResult Create(SwGallery swgallery, HttpPostedFileBase imageUrl)
{
if (ModelState.IsValid)
{
string newFilenameUrl = string.Empty;
if (imageUrl != null)
{
string filename = Path.GetFileName(imageUrl.FileName);
string newFilename = Guid.NewGuid().ToString().Replace("-", string.Empty)
+ Path.GetExtension(filename);
newFilenameUrl = "/Uploads/" + newFilename;
string physicalFilename = Server.MapPath(newFilenameUrl);
imageUrl.SaveAs(physicalFilename);
swgallery.ImageUrl = newFilenameUrl;
}
db.SwGalleries.Add(swgallery);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.Menu_Fk = new SelectList(db.SwMenus, "Id", "Name", swgallery.Menu_Fk);
return View(swgallery);
}