I have two forms on my ASP.Net View,
@using (Html.BeginForm("saveData", "Home", FormMethod.Post, new { id = "validate" }))
{
<div class="formular-container">
<div class="row">
<div class="col-md-3">
@Html.LabelFor(x => x.ccBesucherFirma)
</div>
<div class="col-md-7">
@Html.TextBoxFor(x => x.ccBesucherFirma, new { @class = "form-control firma" })
@Html.ValidationMessageFor(x => x.ccBesucherFirma)
</div>
</div>
<div class="row">
<div class="col-md-3">
@Html.LabelFor(x => x.ccBesucherName)
</div>
<div class="col-md-7">
@Html.TextBoxFor(x => x.ccBesucherName, new { @class = "form-control name" })
@Html.ValidationMessageFor(x => x.ccBesucherName)
</div>
</div>
<div class="row">
<div class="col-md-3">
@Html.LabelFor(x => x.ccBesucherDatum)
</div>
<div class="col-md-7">
@Html.TextBoxFor(x => x.ccBesucherDatum, new { @class = "form-control datum", ID = "datepicker", placeholder = "" })
@Html.ValidationMessageFor(x => x.ccBesucherDatum)
</div>
</div>
<div class="row">
<div class="col-md-3">
@Html.LabelFor(x => x.ccAnsprechpartnerTurck)
</div>
<div class="col-md-7">
@Html.TextBoxFor(x => x.ccAnsprechpartnerTurck, new { @class = "form-control contact" })
@Html.ValidationMessageFor(x => x.ccAnsprechpartnerTurck)
</div>
</div>
<button class="btn btn-default" id="entry" type="submit">Eintragen</button>
</div>
}
@using (Html.BeginForm("editData", "Home", FormMethod.Post, new { id = "validate" }))
{
<div id="overlay">
<div id="overlay-container">
<table class="overlay-table">
<tr>
<td>@Html.LabelFor(x => x.ccBesucherFirma)</td>
<td>@Html.TextBoxFor(x => x.ccBesucherFirma, new { @class = "form-control overlay-input firma", disabled = "" })</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.ccBesucherName)</td>
<td>@Html.TextBoxFor(x => x.ccBesucherName, new { @class = "form-control overlay-input name", disabled = "" })</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.ccBesucherDatum)</td>
<td>@Html.TextBoxFor(x => x.ccBesucherDatum, new { @class = "form-control overlay-input datum", id = "datepicker", disabled = "" })</td>
</tr>
<tr>
<td>@Html.LabelFor(x => x.ccAnsprechpartnerTurck)</td>
<td>@Html.TextBoxFor(x => x.ccAnsprechpartnerTurck, new { @class = "form-control overlay-input contact", disabled = "" })</td>
</tr>
</table>
<div id="EditResult"></div>
<button class="btn btn-default overlay-button" type="button" id="print">Ausweis Drucken</button>
<button class="btn btn-default overlay-button" type="button" id="edit">Bearbeiten</button>
<button class="btn btn-default close-overlay overlay-button" type="reset" value="xModel">Schließen</button>
</div>
</div>
}
One to save the data to the database and the other to edit mistakes in the data. I read that it would possible to use two forms, without a problem so I just put another form in my view and hoped for the best. Now my problem is, that the second form won't post any data to my controller. For this I tried alot of ways but none succeeded. Here are my tries:
First I tried to use a form like you see above in my View. I need to mention something with the form, I'm editing the edit button with jquery so that it gets the attribute type="submit"
because I don't want to delete and create the button.
In Javascript/Jquery with ajax:
var firma = $(".firma").val();
var name = $(".name").val();
var datum = $(".datum").val();
var contact = $(".contact").val();
var formData = [firma,name,datum,contact];
$.ajax({
url: "/Home/editData",
type: "POST",
dataType: "json",
data: JSON.stringify(formData),
success: function(myData) {
$("#EditResult").html(myData);
}
});
And at last my Controller, where the data should end up:
[HttpPost]
public ActionResult editData(BesucherausweisModel xFormData)
{
var a = xFormData;
return View("Index");
}
EDIT: Here's the class:
[Required]
[Display(Name = "Firma")]
public string ccBesucherFirma { get; set; }
[Required]
[Display(Name = "Name")]
public string ccBesucherName { get; set; }
[Required]
[Display(Name = "Datum")]
public string ccBesucherDatum { get; set; }
[Required]
[Display(Name = "Ansprechpartner")]
public string ccAnsprechpartnerTurck { get; set; }
Can anybody tell what I'm doing wrong here?
EDIT: Ok, my Instructor told me to not work on this project anymore, I still thank you for your help.
I believe you serialization misses properties names. Add unique ids to your forms and try this instead:
$.ajax({
url: "/Home/editData",
type: "POST",
dataType: "json",
data: $('#some-unique-id').serialize(),
success: function(myData) {
$("#EditResult").html(myData);
}
});
Hi you are in right direction, only need some correction in ajax call. Find below code:
step 1) Confirm your BesucherausweisModel having public properties for below attributes:
class BesucherausweisModel
{
public string Firma {get; set;}
public string Name {get; set;}
public string Datum {get; set;}
public string Ansprechpartner {get; set;}
}
step 2) pass data by ajax call like below way:
var xFormData = {};
xFormData.Firma = $(".firma").val();
xFormData.Name = $(".name").val();
xFormData.Datum = $(".datum").val();
xFormData.Ansprechpartner = $(".contact").val();
$.ajax({
url: "/Home/editData",
type: "POST",
dataType: "json",
contentType: "application/json",
data: JSON.stringify(xFormData),
success: function(myData) {
$("#EditResult").html(myData);
}
});
Please let me know, is this works for you?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.