Endlich habe ich dieses Problem gelöst.
i eine normale Form geschaffen hatte, die einige Eigenschaften und Bilddatei unter ihnen hatte:
<h1 class="block-header">افزودن گروه</h1>
<div class="separator"></div>
<div class="block-standart">
<div class="form inline">
<fieldset>
<legend>Product</legend>
<div class="group">
@Html.LabelFor(model => model.Product.Name, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.Name, new { @class = "text", id = "Name" })
@Html.ValidationMessageFor(model => model.Product.Name)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Description, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.Description, new { @class = "text", id = "description" })
@Html.ValidationMessageFor(model => model.Product.Description)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Keyword, new { @class = "label" })
<div class="controls">
<div id="t">
@Html.TextBoxFor(model => model.Product.Keyword, new { @class = "text", id = "Keyword" })
@*<input type="text" name="tags" placeholder="Tags" class="tm-input" />*@
@*<input type="text" data-toggle="tags" id="s" />*@
</div>
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Like, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.Like, new { @class = "text" })
@Html.ValidationMessageFor(model => model.Product.Like)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.DisLike, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.DisLike, new { @class = "text" })
@Html.ValidationMessageFor(model => model.Product.DisLike)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.URL, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.URL, new { @class = "text" })
@Html.ValidationMessageFor(model => model.Product.URL)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Enable, new { @class = "label" })
<div class="controls">
@Html.CheckBoxFor(model => model.Product.Enable, new { @class = "text" })
@Html.ValidationMessageFor(model => model.Product.Enable)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Tags, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.Tags, new { @class = "text", id = "tags" })
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Image, new { @class = "label" })
<div class="controls">
<input type="file" id="UploadImage" name="UploadImage" />
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Summery, new { @class = "label" })
<div class="controls">
@Html.TextAreaFor(model => model.Product.Summery, new { @class = "text", id = "txtSum" })
@Html.ValidationMessageFor(model => model.Product.Summery)
</div>
</div>
<div class="group">
@Html.LabelFor(model => model.Product.Price, new { @class = "label" })
<div class="controls">
@Html.TextBoxFor(model => model.Product.Price, new { @class = "text", id = "price" })
@Html.ValidationMessageFor(model => model.Product.Price)
<input type="text" id="gi" name="Product.GroupId" />
</div>
</div>
<p>
<input type="submit" value="Create" id="btnSabt" onclick="ExceptImage();" />
</p>
<textarea id="e">jg</textarea>
</fieldset>
</div>
</div>
</form>
und mein Controller war:
public ActionResult AddProduct(Product product, HttpPostedFileBase
UploadImage)
{
ProductRepository blProduct = new ProductRepository();
if (ModelState.IsValid)
{
product.Image = UploadImage.FileName;
string path = Server.MapPath("~") + "Files\\UploadImages\\" +
UploadImage.FileName;
UploadImage.InputStream.ResizeImageByWidth(500, path,
Utilty.ImageComperssion.Normal);
if (blProduct.Add(product))
{
return MessageBox.Show("محصول با موفقیت ثبت شد", MessageType.Success);
}
else
{
System.IO.File.Delete(path);
return MessageBox.Show("محصول ثبت نشد", MessageType.Error);
}
}
else
{
//مقدار ورودی اشتباه
return MessageBox.Show(ModelState.GetErrors(), MessageType.Warning);
}
}
I posted Daten in 2 Schritten:
1-i hat alle Daten außer der Datei veröffentlicht.
2-i hat die Datei als separate Datei veröffentlicht.
In Schritt 1 habe ich Onclilck-Ereignis verwendet und eine Funktion namens ExceptImage verwendet.
in ExceptImage Gebraucht i ajax alle Daten mit Ausnahme der Bilddatei zu schreiben:
function ExceptImage(e) {
e.preventDefault();
$.ajax({
url: "/Admin/AddProduct",
type: "post",
data: {
Name: $("#Name").val(),
Description: $("#description").val(),
Keyword: $("#Keyword").val(),
Like: $("#Product_Like").val(),
DisLike: $("#Product_DisLike").val(),
URL: $("#Product_URL").val(),
Enable: $("#Product_Enable").val(),
Summery: $("#txtSum").val(),
Tags: $("#Product_Tags").val(),
GroupId: $("#gi").val()
}
})
}
in Schritt 2 i Form Veranstaltung einreichen verwendet (jQuery) und die Datei geschrieben:
$("#myUploadForm").submit(function (e) {
e.preventDefault();
var fd = new FormData(this)
$.ajax({
url: '/Admin/AddProduct',
type: 'POST',
data: fd,
contentType: false,
cache: false,
processData:false
})
})
Sie verwenden können ['formData'] (https://developer.mozilla.org/en-US/docs/Web/API/FormData) um anzugebende Werte anzuhängen. Sie können auch [eine Datei dazu hinzufügen] (https://developer.mozilla.org/en-US/docs/Web/API/FormData/Using_FormData_Objects) –