I'm totally new with asp.net mvc. I'm having trouble consuming rest web service in asp.net mvc4 app.
我是asp.net mvc的新手。我在asp.net mvc4应用程序中使用休息Web服务时遇到问题。
This is the interface of the service:
这是服务的接口:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "GetRuleDetail/{id}")]
string GetRuleDetail(string id);
}
In my mvc app I've added my service as service reference "ServiceReference1"
在我的mvc应用程序中,我添加了我的服务作为服务参考“ServiceReference1”
Then I've created a controller:
然后我创建了一个控制器:
public ActionResult Index()
{
string strjson = Request["Json"].ToString();
//string strjson = "input={\"name\": \"obj1\",\"x\": 11,\"y\":20,\"obj\":{\"testKey\":\"val\",},\"tab\":[1 , 2, 46]}";
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(strjson));
}
The string strjson, I want to pass it from the view which has the following code:
字符串strjson,我想从具有以下代码的视图中传递它:
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";} <h2>Index</h2> <section class="contact">
<header>
<h3>Enter your JSON string</h3>
</header>
<p>
<ol>
<li>
@Html.Label("Json")
@Html.TextBox("txtJson")
</li>
</ol>
</p>
<p>
<button>Test</button>
</p>
Am I missing something? Cz strjson is always null and the Index() method is executed before I enter my jsonstring in the textbox. How can I fix that plz
我错过了什么吗? Cz strjson始终为null,并且在我在文本框中输入jsonstring之前执行了Index()方法。我怎么能解决这个问题
1 个解决方案
#1
0
This is not right approach first you need to render a view than you will post it to server along with id which will then be passed to service.You need to create a model which will be binded to view.
这不是正确的方法首先你需要渲染一个视图,而不是将它与id一起发布到服务器,然后将其传递给service.You需要创建一个将被绑定以查看的模型。
First render view
首先渲染视图
public ActionResult Index()
{
return View();//Tihs will simply return view
}
This is model class by which view will be binded
这是视图将被绑定的模型类
public class JsonData
{
public string Id { get; set; }
}
This will be your view
这将是你的看法
@model JsonData
@using (Html.BeginForm("GetServiceData", "ControllerName", FormMethod.Post))
{
@Html.Label("Json")
@Html.TextBoxFor(m=>m.Id)
<input type="submit" value="Submit" />
}
Now when you will post this view it will go to controller along with data in textbox
现在,当您发布此视图时,它将与文本框中的数据一起转到控制器
[HttpPost]
public ActionResult GetServiceData(JsonData model)
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(model.Id));//Tihs will simply return view
}
#1
0
This is not right approach first you need to render a view than you will post it to server along with id which will then be passed to service.You need to create a model which will be binded to view.
这不是正确的方法首先你需要渲染一个视图,而不是将它与id一起发布到服务器,然后将其传递给service.You需要创建一个将被绑定以查看的模型。
First render view
首先渲染视图
public ActionResult Index()
{
return View();//Tihs will simply return view
}
This is model class by which view will be binded
这是视图将被绑定的模型类
public class JsonData
{
public string Id { get; set; }
}
This will be your view
这将是你的看法
@model JsonData
@using (Html.BeginForm("GetServiceData", "ControllerName", FormMethod.Post))
{
@Html.Label("Json")
@Html.TextBoxFor(m=>m.Id)
<input type="submit" value="Submit" />
}
Now when you will post this view it will go to controller along with data in textbox
现在,当您发布此视图时,它将与文本框中的数据一起转到控制器
[HttpPost]
public ActionResult GetServiceData(JsonData model)
{
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
return View(obj.GetRuleDetail(model.Id));//Tihs will simply return view
}