0

我有以下视图,其中所有字段都应作为 API 调用的一部分发送:-

@using (Html.BeginForm(
      "CreateProcess","Rack", FormMethod.Post
    ))
     {
    <table width="650" bgcolor="#FFFFFF" cellpadding="5" cellspacing="0" style="border:1px #ccc solid" align="center">
        <tr>
            <th colspan="4">Save Asset Form</th>
        </tr>
        <tr>
            <td align="right" width="142">Name</td>
            <td><input type="text" class="txt" NAME="assetName" style="width:160px;" /></td>
            <td width="142" align="right">Asset Type</td>
            <td><input type="text" class="txt" NAME="assetType" value="" style="width:160px;" /></td>
        </tr>
        <tr>
            <td align="right" width="142">Product Name</td>
            <td><input type="text" class="txt" NAME="productName" style="width:160px;" /></td>
            <td align="right" width="142">Asset Tag</td>
            <td><input type="text" class="txt" NAME="resTag" style="width:160px;" /></td>
        </tr>
        <tr>
            <td colspan="4">
            <table class="innertable" cellpadding="5" cellspacing="0" width="100%">
            <tr>
                <td class="thd" colspan="4">Resource Info</td>
            </tr>
            <tr>
                <td align="right" width="142">Asset Serial Number</td>
                <td><input type="text" class="txt" NAME="resSlNo" style="width:160px;" /></td>
                <td width="142" align="right">BarCode</td>
                <td><input type="text" class="txt" NAME="barCode" value="" style="width:160px;" /></td>
            </tr>
            <tr>
                <td align="right" width="142">Location</td>
                <td><input type="text" class="txt" NAME="location" style="width:160px;" /></td>
                <td width="142" align="right">Purchase Cost</td>
                <td><input type="text" class="txt" NAME="purchaseCost" value="" style="width:160px;" /></td>
            </tr>
            <tr>

                <td class="thd" colspan="4">Authentication details</td>
            </tr>
            <tr>
                <td align="right" width="142">username</td>
                <td><input type="text" class="txt" NAME="username" style="width:160px;" /></td>
                <td width="142" align="right">password</td>
                <td><input type="password" class="txt" NAME="password" value="" style="width:160px;" /></td>
            </tr>
            <tr>
                <td align="right" width="142">domain</td>
                <td><input type="text" class="txt" NAME="DOMAIN_NAME" style="width:160px;" /></td>
                <td align="right" width="142">Authentication Mode</td>
                <td><INPUT TYPE=radio NAME="logonDomainName" value="Local Authentication" >Local Authentication&nbsp;
                <INPUT TYPE=radio NAME="logonDomainName" value="AD_AUTH" >AD Authentication</td>
            </tr>
            </table>
            </td>
        </tr>
        <tr>
            <td colspan="4" height="10"></td>
        </tr>
        <tr>
            <td align="center" colspan="4" class="footer"><input type="submit" name="operation" value="SaveAsset" class="btnsubmitact"></td>
        </tr>
        </table>
    }

我的操作方法如下:-

[HttpPost]
        public ActionResult CreateProcess(Rack rack)
        {

            using (var client = new WebClient())
            {

                try
                { 
                    var query = HttpUtility.ParseQueryString(string.Empty);

var url = new UriBuilder("http://localhost:8080/jw/web/json/Process/create/" //notsure what to write here…. );
                    url.Query = query.ToString();
                    string json = client.DownloadString(url.ToString());

但是我如何在我的操作方法中构造我的 API URL,以便它将所有模型绑定器值作为参数,例如

http://localhost:8080/jw/web/json/Process/create?parm1=&parm2=&etc....
4

1 回答 1

1

您可以从Form集合中阅读它们:

[HttpPost]
public ActionResult CreateProcess(Rack rack)
{
    using (var client = new WebClient())
    {
        client.Headers[HttpRequestHeader.Accept] = "application/json";
        var query = HttpUtility.ParseQueryString(string.Empty);
        foreach (string key in this.Request.Form)
        {
            query[key] = this.Request.Form[key];
        }
        var url = new UriBuilder("http://localhost:8080/jw/web/json/Process/create");
        url.Query = query.ToString();
        try
        {
            string json = client.DownloadString(url.ToString());
        }
        catch (WebException ex)
        {
            ...
        }
    }

    ...
}
于 2013-07-31T11:45:04.663 回答