我有一个有效的上传和保存表单,但我想通过一个按钮传递上传的图像并保存电影的其他数据(合并保存和上传)
这是我的 cshtml 创建视图:
@model MvcMovie.Models.Movie
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
<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.ReleaseDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReleaseDate)
@Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Genre)
@Html.ValidationMessageFor(model => model.Genre)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Price)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Price)
@Html.ValidationMessageFor(model => model.Price)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Rating)
@Html.ValidationMessageFor(model => model.Rating)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ImageUrl)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ImageUrl)
@Html.ValidationMessageFor(model => model.ImageUrl)
</div>
<input type="submit" value="Create" />
</fieldset>
<img src="~/Content/Images/Full/image1.JPG" alt="Sample Image" width="300px" height="200px" />
}
@using (Html.BeginForm("Upload", "Movies", FormMethod.Post, new { enctype="multipart/form-data" }))
{
<input name="ImageUploaded" type="file">
<input type="submit" value="Upload"/>
}
@Html.ActionLink("Back to List", "Index")
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
这是我的控制器:
public ActionResult Create()
{
return View();
}
//
// POST: /Movies/Create
[HttpPost]
public ActionResult Create(Movie movie)
{
if (ModelState.IsValid)
{
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index", "Movies");
}
return View(movie);
}
public ActionResult Edit(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
[HttpPost]
public ActionResult Upload(ImageModel model)
{
if (ModelState.IsValid)
{
string fileName = model.ImageUploaded.FileName;
string serverPath = Server.MapPath("~");
string imagesPath = serverPath + "Content\\Images\\";
String fullPath = @"" + "C:/Users/FAZI-7/Desktop/Full/";
String thumbPath = @"" + "C:/Users/FAZI-7/Desktop/Thumb/";
ImageModel.ResizeAndSave(thumbPath, fileName, model.ImageUploaded.InputStream, 80, false);
ImageModel.ResizeAndSave(fullPath, fileName, model.ImageUploaded.InputStream, 600, false);
}
return RedirectToAction("Index", "Movies");
}
我不能为这两个功能制作一个按钮,我已经读到我可能需要具有两个参数的相同控制器操作,或者将所有控件放在一种形式中,但是我该怎么做呢?