0

I have the following view, where all its fields should be sent as part of an API call:-

@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>
    }

My action method is as follow:-

[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());

But how I can construct my API URL inside my action method, so that it will have all the model binder values as parameters such as

http://localhost:8080/jw/web/json/Process/create?parm1=&parm2=&etc....
John John
  • 1
  • 72
  • 238
  • 501

1 Answers1

1

You could read them from the Form collection:

[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)
        {
            ...
        }
    }

    ...
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 1
    :) I was just about to add a comment with a link to this : http://stackoverflow.com/questions/17096201/build-query-string-for-system-net-httpclient-get – Jason Evans Jul 31 '13 at 11:47