如何在控制器中为图像制作编辑方法 [英] How to make an edit method in the controller for image
本文介绍了如何在控制器中为图像制作编辑方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我已经制作了多个上传的图像,它运行得很好。但是如何进行编辑呢?我有一个可以有多个图像的学生模型,如何从我想要更改的图片中获取值?
是否可以从中获取值[HttpGet]我的编辑视图中有哪些照片,如何查看我选择的照片?
这是我的照片模型:
Hi
I have made an multiple upload af image and it works just fine. But how do I make an edit? I have a student model that can have multiple images, how do I get the value from that picture I want to change?
is it possible to get the values from the [HttpGet] which photos there is in my edit view and how can I check which photo I have selected?
This is my Photo model :
public class Photo
{
public int PhotoId { get; set; }
public String ProfileImagePath { get; set; }
public int StudentId { get; set; }
public virtual Student Student { get; set; }
public void SaveImage(HttpPostedFileBase image,
String serverPath, String pathToFile)
{
if (image == null) return;
string filename = Guid.NewGuid().ToString();
ImageModel.ResizeAndSave(
serverPath + pathToFile, filename,
image.InputStream, 200);
ProfileImagePath = pathToFile + filename +
".jpg";
}
}
学生型号:
The Student model :
public class Student
{
public int StudentId { get; set; }
public String Name { get; set; }
public String Adress { get; set; }
public virtual ICollection<Photo> Photos { get; set; }
}
这是我在StudentsController中的编辑:
Thise is my edit in StudentsController :
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Student student = db.Students.Find(id);
if (student == null)
{
return HttpNotFound();
}
return View(student);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "StudentId,Name,Adress, PhotoId, ProfileImagePath, StudentId")] Student student, HttpPostedFileBase image, Photo photoFile, FormCollection _collection)
{
if (ModelState.IsValid)
{
var hiddenId = _collection["photoId"];
var photoPath = _collection["photoPath"];
if (student.StudentId == default(int))
{
db.Students.Add(student);
photoFile.SaveImage(image, HttpContext.Server.MapPath("~"), "/ProfileImages/");
}
else
{
db.Entry(student).State = EntityState.Modified;
photoFile.SaveImage(image, HttpContext.Server.MapPath("~"), "/ProfileImages/");
}
db.SaveChanges();
return RedirectToAction("Index");
}
return View(student);
}
编辑观点:
The view for edit :
@model test4.Models.Student
@foreach (var item in Model.Photos)
{
@Html.Hidden("photoId", item.PhotoId);
<img src="@item.ProfileImagePath" alt="photo"/>
<input type="file" name="image" multiple="multiple" value="@item.PhotoId" />
@*}*@
}
}
推荐答案
我我同意Sreekanth,正如他所说:
你需要根据索引在循环中创建不同的隐藏字段。喜欢:< input type =hiddenname =hid+ @ item.index ++ value =@ item.PhotoId/>
然后你需要使用已编辑图像的ID并在控制器的动作中更新相同的图像内容。
我没有做同样的事情但可能是我的文章通过为您提供单个图像的更新方案将有所帮助:
按文件浏览,拖动和放大图像上传图像在ASP.NET MVC中删除和使用Webcam [ ^ ]
谢谢。
Hi, I am agree with Sreekanth, as he said:
You need to create different hidden fields within the loop based on index. like : <input type="hidden" name="hid"+ @item.index++ value="@item.PhotoId" />
Then you need to use the id of edited image and update the same image content in Action of your Controller.
I have not done the same but may be my article would help a little by giving you update scenario for single image:
Uploading Image by File Browsing, Dragging & Dropping and Using Webcam in ASP.NET MVC[^]
Thanks.
这篇关于如何在控制器中为图像制作编辑方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文