繁体   English   中英

WEB API MVC4-使用datagrid发布

[英]WEB API MVC4 - using datagrid to post

我已经成功编写了一个MVC4 webapi,它可以使用CompanyID,名称并将其发布到DB。 我需要进行更改,以使用户可以使用其名称为CompanyID输入多个值,然后一次性将其保存到DB。 我怎样才能做到这一点? 这是我的代码

--Index.cshtml
<div id="body">
    <section class="featured">
        <div class="content-wrapper"> 
            <hgroup class="title">
                <h1>Welcome Web API!</h1>
            </hgroup>
        </div>
    </section>
    <section class="content-wrapper main-content clear-fix">
        <p>
            For API documentation: @Html.ActionLink("API", "Index", "Help", new { area = "" }, null)
        </p>
        <p>
            Update/Insert Company info: <a href="Companies.html">Click Here!</a>
        </p>
           </section>
</div>

--Companies.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>
<body>
    <script>
        var overrides = Array();
        function parseform(button) {
            var CompanyID = $("#CompanyID").val();
            var CompanyName = $("#CompanyName").val();

            var lst = {
               CompanyID: CompanyID,
               CompanyName: CompanyName
            }

            if (button.attr('value') === "POST") {
                console.log("posting : " + lst.toString());
                postdata(lst);
            } else {
                console.log("ERROR");
            }
        }

        function postdata(lst) {

            $("#response").text("Posted");
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "api/Company/",
                data: lst,
                xhrFields: {
                    withCredentials: true
                },
                success: function (data, status, xhr) {
                    console.log(status);   
                    $("#response").text(status)
                },
                error: function (xhr, status, error) {
                    console.log(xhr.responseText);
                    var json = jQuery.parseJSON(xhr.responseText);
                    console.log(json);
                    $("#response").text(status)
                    alert(json.Message);
                }
            });
        }

        $(document).ready(function () {
            $('input:button').click(function () {
                parseform($(this));
            });
        });

    </script>

    <div id="form">
            <label for="CompanyID">CompanyID:</label><input type="text" id="CompanyID" /><br />
           <label for="CompanyName">CompanyName:</label><input type="text" id="CompanyName" /><br />
            <input type="button" id="Post" value="POST"/>
    </div>

    <div id="response">

    </div>
</body>
</html

在CompaniesController中,我为POST方法编写了一个对存储过程的调用,以一次保存一条记录。 我如何一次发布许多行,我应该对html文件进行哪些更改。

谢谢R

您如何在模型中声明变量。 对于操作多值类型,请尝试如下操作:

private ICollection<string> _variable;
public ICollection<string> VariableName
{
    get
    {
        if (_variable == null)
        {
            return new Collection<string> {""};
        }
        else
        {
            return _variable;
        }
    }
    set { _variable = value; }
}

暂无
暂无

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

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