简体   繁体   English

如何将多个值传递给MVC控制器

[英]How to pass multiple values to MVC controller

I have a table with various pieces of data. 我有一张包含各种数据的表格。 I also have a Html.TextAreaFor for to type text in. Here is the table 我还有一个Html.TextAreaFor用于输入文本。这是表格

I need to capture the text in the Textarea for .. send it to the controller via ajax .post 我需要捕获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>

Here is the ajax call 这是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');
                }
            });

I'm passing the ID of the row and the text in the text box. 我正在传递行的ID和文本框中的文本。 Here is the controller I'm passing to 这是我要传递的控制器

  [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");
    }

The problem: 问题:

Doesn't work. 不行。 It doesn't pass the values to the controller. 它不会将值传递给控制器​​。 It's null every time. 它每次都是空的。 I've tried a ton of different ajax calls.. Nothing works. 我已经尝试了大量不同的ajax调用..没有任何作用。 I can capture the Id, but not the text and when it reaches the controller it is null. 我可以捕获Id,但不能捕获文本,当它到达控制器时它是空的。 If these are wrong, how do I simply send the Id of the row.. and the text typed in the textbox to the controller. 如果这些是错误的,我如何简单地将行的Id ...和文本框中键入的文本发送到控制器。 ?

The data that passes a string, as well as the parameter that you receive as completedPolicy, is empty and is not a part of it. 传递字符串的数据以及您作为completedPolicy接收的参数为空,并且不是它的一部分。

your Data : 你的数据:

"{"Id":"id","polNotes":" policyNotes"}"

data shoud be a json type 数据应该是json类型

{ Id: 'id', polNotes:' policyNotes' }

this no need : 这没有必要:

JSON.stringify({ Id: 'id', polNotes:' policyNotes' });

and action shoud like: 和行动应该像:

[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 : ajax:

 var data = { Id: id, polNotes: policyNotes };

            $.ajax({
                url: '/Policy/CompleteNotes',
                type: 'POST',
                contentType: 'application/json',
                data: data,
                success: function () {
                    alert('success');
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将表单值传递给 .NET MVC 中的 Controller - How to pass Form Values to the Controller in .NET MVC 在MVC / Razor中,如何获取多个复选框的值并将它们全部传递给控制器​​? - In MVC/Razor, how do I get the values of multiple checkboxes and pass them all to the controller? 从视图传递多个值/参数到MVC控制器 - pass multiple values/parameters from view to mvc controller shorter way 如何将字符串值从类文件传递到Mvc控制器 - How to pass string values from Class file to Mvc Controller 如何将动态创建的文本框值从视图传递到MVC中的控制器 - How to pass dynamically created textbox values from view to controller in MVC 如何将 Javascript 创建的动态文本框值传递给 MVC 中的控制器? - How to pass Dynamic textbox values that is created by Javascript to controller in MVC? 在MVC 6中,如何在视图中编码复选框列表并将选中的值传递给控制器​​? - In MVC 6, how to code checkbox list in view and pass the checked values to the controller? 如何将值从View传递到Controller Action参数? MVC3 - How to pass values from View to a Controller Action parameter? MVC3 如何将值传递给所有请求的C#MVC控制器 - How to pass values to all requested c# MVC controller 如何将ID传递给MVC中的控制器 - how to pass id to controller in mvc
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM