如何从引导模式获取值并将它们发送到控制器C#MVC

时间:2017-05-17 11:08:15

标签: javascript c# asp.net-mvc modal-dialog

按钮点击我打开带有3个输入的模态,其中一个是下拉列表,我可以选择一些活动用户,另外两个输入是输入,我可以为该用户写一些标题和描述我在下拉列表中被选中。现在我想要做的是从模态中选择信息并将它们发送到我的控制器(c#),我想我必须创建javascript对象并将选定用户,标题和描述的对象发送到我的C#Controller。

这是我的模态代码:

<div class="modal-content">
            <div class="modal-header btn-info" style="font-weight:bold;color:white;">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h5 class="modal-title modal-sm">Title</h5>
            </div>
            <div class="modal-body">
                <form id="formCompose" class="form-horizontal form-label-left input_mask" method="post">
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3">User</label>

                        <div class="col-md-9 col-sm-9">
                            @Html.DropDownList("UserID", new SelectList(ViewBag.Users, "Id", ".Name"), "Name", new { @class = "form-control" })
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3">Title</label>

                        <div class="col-md-9 col-sm-9">
                            <input type="text" class="form-control">
                            <span class="fa fa-comment form-control-feedback right" aria-hidden="true"></span>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3 col-sm-3">Description</label>

                        <div class="col-md-9 col-sm-9">
                            <textarea id="message" rows="7" required="required" class="form-control" name="message"
                                      data-parsley-trigger="keyup" data-parsley-minlength="20"
                                      data-parsley-maxlength="100"
                                      data-parsley-minlength-message="You need to enter at least a 20 caracters long description comment.."
                                      data-parsley-validation-threshold="10"></textarea>

                            <span class="fa fa-language form-control-feedback right" aria-hidden="true"></span>

                        </div>
                    </div>
                </form>
            </div>

以下是我用javascript尝试的内容,如果我们可以调用它甚至尝试:

<script type="text/javascript">
    var user = {User: "I need Id from dropdown here probably", Title: "Title from modal ", Description: "Description from modal" };
</script>

我真的不知道如何从输入值获取值并将它们发送到c#控制器。任何形式的帮助都会很棒!

谢谢你们 干杯

1 个答案:

答案 0 :(得分:1)

您必须在表单中添加action属性才能提交action中的controller

<form id="formCompose" action="@Url.Action("MyAction", "MyController")"  class="form-horizontal form-label-left input_mask" method="post">

name属性的值应与Property模型名称相同

<label class="control-label col-md-3 col-sm-3">Title</label>

                        <div class="col-md-9 col-sm-9">
                            <input type="text" name="Title" class="form-control">
                            <span class="fa fa-comment form-control-feedback right" aria-hidden="true"></span>
                        </div>

并将name="message"更改为name="Description",将"UserID"更改为"User",并在模型中添加submit button以提交表单并将数据发送至{ {1}}