将值从MVC视图传递到控制器

时间:2017-06-16 10:45:18

标签: javascript asp.net-mvc controller

我的MVC应用程序中有一个控制器,它将数据插入数据库。这看起来如下:

 public ActionResult Add(string orgCode)
    {
       ClientClass client = new ClientClass();
        if (ModelState.IsValid)
        {
            int _records = client.Insert(orgCode, "");
          //  int _records = insertmodel.Insert(insertmodel.Org_Name, insertmodel.Org_Code);
            if (_records > 0)
            {
                return RedirectToAction("Index", "Home");
            }
            else
            {
                ModelState.AddModelError("", "Can Not Insert");
            }
        }
        return View(client);
    }

我的视图层中还有一些输入字段,用于存储我输入的数据的ng-model,如下所示:

<div id="step-2" ng-controller="HomeController">
                    <div class="form-group">
                        <div class="form-group" style="padding-left:33%">
                            <div class="row">
                                <br />
                                <div class="col-lg-2">
                                    <h2 style="color:#FF5D00">STEP 2:</h2>
                                </div>
                                <div class="col-lg-5">
                                    <h2 style="color:#00386B"> Contact Details</h2>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-2" style="padding-top:8px">
                                    <label for="contactperson" class="labelQuestion">Contact Name</label>
                                </div>
                                <div class="col-lg-7">
                                    <input type="text" class="form-control" name="contactperson" ng-model="userDetails.contactperson" id="txtContactPerson" placeholder="e.g John Smith">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-2" style="padding-top:8px">
                                    <label for="contactemail" class="labelQuestion">Contact Email</label>
                                </div>
                                <div class="col-lg-7">
                                    <input type="text" class="form-control" name="contactemail" ng-model="userDetails.contactemail" id="txtContactEmail" placeholder="e.g john.smith@go2tigers.com">
                                    <div class="help-block with-errors"></div>
                                </div>
                            </div>

                            <div class="row">
                                <div class="col-lg-2" style="padding-top:8px">
                                    <label for="contactnumber" class="labelQuestion">Contact Number</label>
                                </div>
                                <div class="col-lg-7">
                                    <input type="text" class="form-control" name="contactnumber" id="txtContactNumber" ng-model="userDetails.contactnumber" placeholder="e.g +27 76 256 0098">
                                    <div class="help-block with-errors"></div>
                                </div>
                                <button type="submit">submit stuff</button>
                            </div>
                        </div>
                    </div>
                    <pre>{{userDetails |json }} </pre>
                    {{message}}
                </div>

在我的javascript中,我正在测试Angularjs绑定是否正常工作:

$scope.userDetails = { organisationname: '', organisationcode: '', branchcode: '', department: '', daterange: '', contactperson: '', contactemail: '', contactnumber: '' }

我要做的是将我收集的值传递给添加到数据库的方法。经过4个小时的挣扎,我迷失了。我不知道从哪里调用该方法(javascript或从视图)以及如何通过这些参数。任何援助将不胜感激。

1 个答案:

答案 0 :(得分:0)

你应该分两部分分解你的工作:

1 - 创建Web服务以返回数据(例如JSON)。

请参阅此处的示例:https://spring.io/guides/gs/rest-service/

2 - 在您的Javascript代码(在Angular内)使用HTTP调用该服务,并将收到的JSON绑定到您的范围。

参见示例:https://api.jquery.com/jquery.get/

有任何疑问,请发表评论。

相关问题