简体   繁体   English

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

[英]Passing values from MVC view to controller

I have a controller in my MVC application which inserts data into a database. 我的MVC应用程序中有一个控制器,可将数据插入数据库。 This looks like the following: 如下所示:

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

I also have some input fields in my view layer which store the ng-model for my data that I'm inputting which looks like this: 我的视图层中也有一些输入字段,用于存储我正在输入的数据的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>

In my javascript, I am testing to see if the Angularjs binding is working: 在我的JavaScript中,我正在测试以查看Angularjs绑定是否有效:

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

What I am trying to do is pass those values that I am collecting to the method which adds to the database. 我想要做的是将我收集的那些值传递给添加到数据库的方法。 After 4 hours of struggling I am lost. 经过4小时的奋斗,我迷路了。 I don't know where to call the method from (javascript or from the view) and how to pass those parameters through. 我不知道从哪里(从javascript或从视图)调用该方法以及如何传递这些参数。 Any assistance would be appreciated. 任何援助将不胜感激。

You should break down your work in two parts: 您应该将工作分为两个部分:

1 - Creating a web service in order to return data (eg a JSON). 1-创建Web服务以返回数据(例如JSON)。

See example here: https://spring.io/guides/gs/rest-service/ 在此处查看示例: https//spring.io/guides/gs/rest-service/

2 - Call that service using HTTP on your Javascript code (inside Angular), and binding the received JSON to your scope. 2-在您的Javascript代码(在Angular内部)上使用HTTP调用该服务,并将接收到的JSON绑定到您的范围。

See example: https://api.jquery.com/jquery.get/ 查看范例: https//api.jquery.com/jquery.get/

Any doubts, leave a comment. 如有任何疑问,请发表评论。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM