我有一个包含各种数据的表。我也有一个Html.TextAreaFor用于输入文本。这是表格
我需要捕获Textarea中的文本以用于..通过ajax .post将其发送到控制器
<table class="table table-striped table-hover col-xl-12" id="policyTable">
<tr class="thead-dark">
<th class="toggleMe1">
UnderWriter
</th>
<th class="toggleMe2">
@Html.DisplayNameFor(model => model.Client)
</th>
<th>
Exp-Policies
</th>
<th>
Exp-Date
</th>
<th class="hideMe">
Date - Updated
</th>
<th>
Reviewed-By
</th>
<th>
Email-Created
</th>
<th>
@Html.DisplayNameFor(model => model.CAB)
</th>
<th>
@Html.DisplayNameFor(model => model.LossRun)
</th>
<th>
@Html.DisplayNameFor(model => model.MMS)
</th>
<th>
@Html.DisplayNameFor(model => model.Notes)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr id="id_@item.Id">
<td class="hideMe">
@Html.DisplayFor(modelItem => item.Id, new { id = "VisibleID" })
</td>
<td>
@Html.DisplayFor(modelItem => item.NewUw)
</td>
<td>
@Html.DisplayFor(modelItem => item.Client)
</td>
<td>
@Html.DisplayFor(modelItem => item.Expiring_Policies)
</td>
<td>
@Html.DisplayFor(modelItem => item.ExpDate)
<td class="hideMe">
@Html.DisplayFor(modelItem => item.DateUpdated)
</td>
<td>
@Html.DisplayFor(modelItem => item.InsertedBy)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmailCreated)
</td>
<td>
@Html.DisplayFor(modelItem => item.CAB)
</td>
<td>
@Html.DisplayFor(modelItem => item.LossRun)
</td>
<td>
@Html.DisplayFor(modelItem => item.MMS)
</td>
<td style="width: 25%; height: 120px;">
<div class="form-group">
@* @Html.LabelFor(model => model.Notes, htmlAttributes: new { @class = "control-label col-md-2" })*@
<div class="col-md-12">
@Html.TextAreaFor(modelItem => item.Notes, new { @class = "form-control textAreaCS", @rows = 8 })
@Html.ValidationMessageFor(modelItem => item.Notes, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-12 ml-3">
<div class="row">
<input type="submit" value="Save" id="notes" class="btn btn-outline-primary btn-sm col-2" style="font-size: 16px" />
<div class="col-1">
@if (TempData["LB"] != null)
{
<div class="alert alert-danger text-center text-capitalize" style="font-size: 16px;">
@TempData["LB"]
</div>
}
</div>
</div>
</div>
</td>
<td>
@Html.ActionLink("Review", "EditPolicy", new { id = item.Id }, new { @class = "review" })
</td>
</tr>
}
</table>
这里是ajax调用
var url = "/Policy/CompletedNotes";
var policyNotes = $("#item_Notes").text();
var id = $("#VisibleID").val();
$("input#notes").on('click', function () {
alert(id);
var data = JSON.stringify({ Id: id, polNotes: policyNotes });
$.ajax({
url: '/Policy/CompleteNotes',
type: 'POST',
contentType: 'application/json',
data: data,
success: function () {
alert('success');
}
});
我要在文本框中传递行ID和文本。这是我要传递给的控制器
[HttpPost, ValidateInput(false)]
public ActionResult CompletedNotes(CompletedPolicyVM completedPolicy, string Id, string polNotes)
{
int val = Convert.ToInt32(Id);
using (Db db = new Db())
{
int id = completedPolicy.Id;
NotesDTO notesDTO = db.notes.Find(id);
if (notesDTO == null )
{
return Content("This policy cannot be found." + Id);
}
//instantiate a stringbuilder variable so I can append the text when inserting in db.
var sb = new StringBuilder();
const char carriageReturn = (char)13;
const char newLine = (char)10;
string insertedDate = "Inserted on " + DateTime.Now.ToString("MM/dd/yyyy HH:mm") + " by " + User.Identity.Name;
if (notesDTO.Notes == null || notesDTO.Notes == String.Empty)
{
notesDTO.Notes = sb.AppendLine(insertedDate).ToString() + " " + polNotes + "\n";
}
else
{
notesDTO.Notes += newLine + sb.AppendLine(insertedDate).ToString() + " " + polNotes + "\n";
}
db.SaveChanges();
}
//Save TempData message
TempData["SM"] = "Policy saved correctly!";
//redirect
return RedirectToAction("CompletedPolicies");
}
问题:
不起作用。它不会将值传递给控制器。每次都为空。我尝试了很多不同的ajax调用。我可以捕获Id,但不能捕获文本,当它到达控制器时,它为null。如果这些是错误的,我该如何简单地将行的ID和在文本框中键入的文本发送给控制器。 ?
答案 0 :(得分:0)
传递字符串的数据以及您作为completedPolicy接收的参数为空,并且不属于其中。
您的数据:
"{"Id":"id","polNotes":" policyNotes"}"
数据应为json类型
{ Id: 'id', polNotes:' policyNotes' }
这没必要:
JSON.stringify({ Id: 'id', polNotes:' policyNotes' });
并且应采取以下行动:
[HttpPost, ValidateInput(false)]
public ActionResult CompletedNotes( int Id, string polNotes)
{
using (Db db = new Db())
{
CompletedPolicyVM completedPolicy=db.CompletedPolicyVM .Find(id);
NotesDTO notesDTO = db.notes.Find(id);
.
.
.
db.SaveChanges();
}
ajax:
var data = { Id: id, polNotes: policyNotes };
$.ajax({
url: '/Policy/CompleteNotes',
type: 'POST',
contentType: 'application/json',
data: data,
success: function () {
alert('success');
}
});