我在控制器中有两个通过ajax调用的动作:
[HttpPost]
public ActionResult Slider(HttpPostedFileBase file)
{
var imagem = System.Web.HttpContext.Current.Request.Files["SliderImage"];
string path = Global.GetAppSetting("CaminhoImagensSlider");
if(imagem != null && imagem.ContentLength > 0)
{
String caminho = Path.Combine(path, imagem.FileName);
db.ImagensSlider.AddObject(new ImagensSlider()
{
Caminho = caminho
});
db.SaveChanges();
Global.SalvarArquivo(caminho, imagem.InputStream);
}
SliderViewModel sliderViewModel = new SliderViewModel(caminhoImagensSlider);
return PartialView("_ImagensSliderPartial", sliderViewModel.Imagens);
}
[HttpPost]
public ActionResult DeletaImagensSlider(int[] imagens)
{
foreach(int id in imagens)
{
ImagensSlider imagemSlider = db.ImagensSlider.Where(i => i.IdImagem == id).Single();
db.ImagensSlider.DeleteObject(imagemSlider);
db.SaveChanges();
}
SliderViewModel sliderViewModel = new SliderViewModel(caminhoImagensSlider);
return PartialView("_ImagensSliderPartial", sliderViewModel.Imagens);
}
第一个动作正常,但第二个动作(DeletaImagensSlider)不起作用。检查Chrome控制台我发现错误:
未找到部分视图'_ImagensSliderPartial'或视图引擎不支持搜索的位置。搜索了以下位置: 〜/查看/ Configuracoes / _ImagensSliderPartial.aspx 〜/查看/ Configuracoes / _ImagensSliderPartial.ascx 〜/查看/共享/ _ImagensSliderPartial.aspx 〜/查看/共享/ _ImagensSliderPartial.ascx 〜/查看/ Configuracoes / _ImagensSliderPartial.cshtml 〜/查看/ Configuracoes / _ImagensSliderPartial.vbhtml 〜/查看/共享/ _ImagensSliderPartial.cshtml 〜/查看/共享/ _ImagensSliderPartial.vbhtml
修改 JS
$("#formSlides").on("submit", function (e) {
e.preventDefault();
var data = new FormData();
var files = $("#imagem").get(0).files;
if (files.length > 0) {
data.append("SliderImage", files[0]);
}
$.ajax(this.action,
{
type: 'POST',
data: $(this).serialize(),
contentType: false,
processData: false,
data: data,
success: function (result) {
$("#imagensContainer").html(result);
}
});
});
$(document).on('change', '[type=checkbox]', function (e) {
if (this.checked) {
imagensChecadas += 1;
imagens.push(this.id);
}
else {
imagensChecadas -= 1;
imagens.remove(this.id);
}
if (imagensChecadas > 0) {
$("#btnDeleteImages").show();
}
else {
$("#btnDeleteImages").hide();
}
});
$("#btnDeleteImages").on("click", function () {
if (confirm("Tem certeza que deseja deletar estas imagens?")) {
$.ajax("/Configuracoes/DeletaImagensSlider", {
type: 'POST',
data: JSON.stringify(imagens),
contentType: 'application/json',
success: function (result) {
$("#imagensContainer").html(result);
}
});
}
});
为什么这个视图在一个动作中找到而在另一个动作中没有?
答案 0 :(得分:0)
我通过放置整个路径来解决问题:"〜/ Areas / Admin / Views / Shared / _ImagensSliderPartial.cshtml"。 但我不明白为什么它在两个行动中以不同的方式起作用......