当前位置: 动力学知识库 > 问答 > 编程问答 >

asp.net mvc 3 - action takes the null object in the image passing of the form a strongly typed view

问题描述:

I'm sorry for my English. Takes a problem with the file transfer (image) to the server. If transferred to a single file from only one form on the page, then everything is fine, but when I try to do several forms (using ajax), data is transmitted at all. Total 7 on the form of images (especially databases) each of which is typified model:

public class Image

{

public int Id { get; set; }

public int ProductId { get; set; }

public int Number { get; set; }

public HttpPostedFileBase Data { get; set; }

public byte[] Img { get; set; }

public string MIMEType { get; set; }

}

Img - property to get data from database, Data - set from page

"Common" view:

@for (int i = 1; i < 8; i++)

{

<div id="[email protected]">

@Html.Partial("EditImage", new Image() { Id = Model.Imgs[i-1], Number = i, ProductId = Model.Id })

</div>

<div class="clear"></div>

}

And main problem, view "EditImage":

@model Image

@using (Ajax.BeginForm("EditImage", "Product", null, new AjaxOptions()

{

Url = "/Product/EditImage",

HttpMethod = "Post",

InsertionMode = InsertionMode.Replace,

UpdateTargetId = "image-" + Model.Number.ToString()

}, new { enctype = "multipart/form-data" }))

{

@Html.HiddenFor(model => model.MIMEType)

@Html.HiddenFor(model => model.ProductId)

@Html.HiddenFor(model => model.Id)

@Html.HiddenFor(model => model.Number)

<div class="mini-image left">

<img src="@Url.Action("Image", "Home", new { Id = Model.Id })" />

</div>

<input type="file" name="Data" />

<input id="[email protected]" type="submit" value="Send"/>

<input id="[email protected]" type="reset" value="Cancel"/>

}

I set breakpoint in first line of action. Properties "ProductId" And "Number" were passed. Data is null :( I tried set id="Data", id="[email protected](Model.Number)" unsuccessfully.

UPDATE: it's harder than i think, Request.Files.Count equals 0

网友答案:

I just need to use some javascript libraries for uploading files by ajax. JQuery ajax functions can't send files somehow.

分享给朋友:
您可能感兴趣的文章:
随机阅读: