Reputation: 143
I want to add product in my website. When I click create a product I must enter some value. When trying to save without any data, you want to check that validation work. It work, but when I click submit the form is not in modal popup (image2) - it display like other site
I want to like this (image1) - display in modal popup
Validation highlighted in red is Polish but it means that the fields are required.
I use formData because I must send a file.
modalform.js
var formdata = new FormData($('form').get(0));
$('form', dialog).submit(function () {
$.ajax({
url: this.action,
type: this.method,
data: formdata,
success: function (result) {
if (result.success) {
$('#myModal').modal('hide');
} else {
$('#myModalContent').html(result);
bindForm();
}
}
});
return false;
});
I'm using AJAX posting to submit the data from my form. When using $(this).serialize() the ajax success is being called but the file is not returning as the content type is different. How can I change this??
Model
public partial class Produkty
{
public int PRO_Id { get; set; }
public string PRO_Nazwa { get; set; }
public string PRO_Jednostka { get; set; }
public float PRO_Vat { get; set; }
public string PRO_Rodzaj { get; set; }
public string PRO_Opis { get; set; }
public string PRO_Waluta { get; set; }
public float PRO_CenaN { get; set; }
public float PRO_CenaB { get; set; }
public string PRO_ZdjecieN { get; set; }
public byte[] PRO_ZdjecieF { get; set; }
}
Create View
@using (Html.BeginForm("Create", "Products", FormMethod.Post, new { enctype = "multipart/form-data" }))
//@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="modal-body">
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.PRO_Nazwa, "Nazwa", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PRO_Nazwa, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PRO_Nazwa, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_Jednostka, "Jednostka", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PRO_Jednostka, new { htmlAttributes = new { @class = "form-control", @Value = "szt." } })
@Html.ValidationMessageFor(model => model.PRO_Jednostka, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_Vat, "Vat", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.DropDownListFor(model => model.PRO_Vat, new List<SelectListItem>
{
new SelectListItem{ Text = "23 %", Value = "23"},
new SelectListItem{ Text = "8 %", Value = "8" },
new SelectListItem{ Text = "5 %", Value = "5" },
new SelectListItem{ Text = "4 %", Value = "4" },
new SelectListItem{ Text = "0 %", Value = "0" }
}, "wybierz...", new { @class = "form-control", @id = "Value1" })
@Html.ValidationMessageFor(model => model.PRO_Vat, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_Rodzaj, "Rodzaj", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.DropDownListFor(model => model.PRO_Rodzaj, new List<SelectListItem>
{
new SelectListItem{ Text = "towar", Value = "towar" },
new SelectListItem{ Text = "usługa", Value = "usługa" }
}, "wybierz...", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PRO_Rodzaj, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_Waluta, "Waluta", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.DropDownListFor(model => model.PRO_Waluta, new List<SelectListItem>
{
new SelectListItem{ Text = "PLN", Value = "PLN" },
new SelectListItem{ Text = "EUR", Value = "EUR" },
new SelectListItem{ Text = "USD", Value = "USD" },
new SelectListItem{ Text = "GBP", Value = "GBP" },
new SelectListItem{ Text = "CHF", Value = "CHF" }
}, "wybierz...", new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.PRO_Waluta, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_CenaN, "Cena netto", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PRO_CenaN, new { htmlAttributes = new { @class = "form-control", @id = "Value2" } })
@Html.ValidationMessageFor(model => model.PRO_CenaN, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_CenaB, "Cena brutto", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PRO_CenaB, new { htmlAttributes = new { @class = "form-control", @id = "MultiplyValue1Value2" } })
@Html.ValidationMessageFor(model => model.PRO_CenaB, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_Opis, "Opis", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
@Html.EditorFor(model => model.PRO_Opis, new { htmlAttributes = new { @class = "form-control", @Value = "brak" } })
@Html.ValidationMessageFor(model => model.PRO_Opis, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PRO_ZdjecieF, "Plik", htmlAttributes: new { @class = "control-label col-md-3" })
<div class="col-md-9">
<input type="file" id="file" class ="btn btn-default btn-file" name="file" />
@Html.ValidationMessageFor(model => model.PRO_ZdjecieF, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal">Anuluj</button>
<input class="btn btn-primary" id="upload" type="submit" value="Zapisz" />
</div>
Controller:
[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Exclude = "PRO_ZdjecieF,PRO_Zdjecie")]Produkty pro, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
//if (imageF != null)
{
pro.PRO_ZdjecieF = new byte[file.ContentLength];
pro.PRO_ZdjecieN = file.ContentType;
file.InputStream.Read(pro.PRO_ZdjecieF, 0, file.ContentLength);
}
db.Produkties.Add(pro);
db.SaveChanges();
return Json(new { success = true });
}
return PartialView("Create", pro);
}
Please help to solve this.
Upvotes: 2
Views: 1545
Reputation: 684
Add the validations in the model class
public partial class Produkty
{
[Required(ErrorMessage = "*")]
public int PRO_Id { get; set; }
[Required(ErrorMessage = "*")]
public string PRO_Nazwa { get; set; }
public string PRO_Jednostka { get; set; }
public float PRO_Vat { get; set; }
public string PRO_Rodzaj { get; set; }
public string PRO_Opis { get; set; }
public string PRO_Waluta { get; set; }
public float PRO_CenaN { get; set; }
public float PRO_CenaB { get; set; }
public string PRO_ZdjecieN { get; set; }
public byte[] PRO_ZdjecieF { get; set; }
}
Add this code in your view page
$('#form1').removeData('validator');
$('#form1').removeData('unobtrusiveValidation');
$.validator.unobtrusive.parse('#form1');
Upvotes: 1