我是 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"))
)
)
我正在使用我创建的包含剑道上传的 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。
有没有什么办法解决这一问题?
提前致谢。