我是ASP.NET MVC的Kendo UI新手,当我尝试通过Kendo Grid内部的Kendo Upload控件发送和使用HTTP请求进行映像时,我遇到了问题。
以下是我网格的代码:
@(Html.Kendo().Grid<RedeemViewModel>()
.Name("RedeemGrid")
.Columns(columns =>
{
columns.Bound(c => c.image).EditorTemplateName("image");
columns.Bound(c => c.Title);
columns.Bound(c => c.Description);
columns.Bound(c => c.Points);
columns.Command(command => command.Edit());
})
.ToolBar(toolbar =>
{
toolbar.Create();
})
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Model(model => model.Id(field => field.Key))
.Read(read => read.Action("GetRedeems", "Redeem"))
.Create(create => create.Action("AddRedeem", "Redeem"))
.Update(update => update.Action("UpdateRedeem", "Redeem"))
)
)
我正在使用我创建的包含Kendo Upload的EditorTemplate。这是:
@model string
@(Html.Kendo().Upload()
.Name("image")
.Multiple(false)
)
这是RedeemViewModel,我正在尝试接收已发布的图片:
public class RedeemViewModel
{
public Guid? Key { get; set; }
public HttpPostedFileBase image { get; set; }
[Required]
public string Title { get; set; }
[Required]
public string Description { get; set; }
[Required]
public int Points { get; set; }
}
这是我的Action方法的代码,网格目标
public ActionResult AddRedeem([DataSourceRequest] DataSourceRequest request, RedeemViewModel redeemViewModel)
{
try
{
if (ModelState.IsValid)
{
// create a new redeem object
Redeem redeem = new Redeem
{
Id = Guid.NewGuid(),
Description = redeemViewModel.Description,
Points = redeemViewModel.Points,
Title = redeemViewModel.Title
};
// try to save the image
var imageSaved = StorageService.SaveImage(redeemViewModel.image, ImageCategory.Redeem, redeem.Id.ToString());
// check if image was saved
if (imageSaved)
{
// update the redeem's image property
redeem.Image = redeem.Id + Path.GetExtension(redeemViewModel.image.FileName);
}
// persist the redeem object to the database
redeem = RedeemService.Add(redeem);
redeemViewModel.Key = redeem.Id;
}
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}
以下是实际发送的请求:
POST http://localhost:3705/Redeem/AddRedeem HTTP/1.1
Host: localhost:3705
Connection: keep-alive
Content-Length: 64
Accept: */*
Origin: http://localhost:3705
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:3705/Redeem/Index
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
sort=&group=&filter=&Key=&Title=test&Description=test&Points=200
当我调试Action方法时,我发现RedeemViewModel的image属性始终为null。
有什么方法可以解决这个问题吗?
提前致谢。