Reputation: 133
I have a Class named Server
public class Server
{
[Key]
public int Id { get; set; }
public string ServerName { get; set; }
public string ServerUrl { get; set; }
public int ServerIndex { get; set; }
public string Username { get; set; }
public string Password { get; set; }
[NotMapped]
public string SID { get; set; }
}
In my server controller, I have HTTP post function
[HttpPost]
public Server Add(Server server)
{
return _serverService.Add(server);
}
When my JSON is as follows (value of SID is missing), it is not hitting the above function.
{
"serverIndex": 0,
"serverName": "string",
"serverUrl": "string",
"username": "string",
"password": "string"
}
If I create a new model called SeverPostModel, with out SID and use it in controller it works. Is there a way to use the same existing model. Any help is appreciated.
Upvotes: 0
Views: 1673
Reputation: 21353
Perhaps the issue is related to the method you are using to post data to the action method. And, please check whether the request URL is correct or not? It is better to post the related code to reproduce the problem.
Based on your code and description, I create a sample on my side, and use the Submit Form and JQuery method to transfer data to the action method, both of them works well on my side.
Use the Submit Form method to transfer data:
Code in the controller:
[HttpGet]
public IActionResult Add()
{
return View();
}
[HttpPost]
public Server Add(Server server)
{
return server;
}
Code in the View:
@model Test.Models.Server
@{
ViewData["Title"] = "Add";
}
<div class="row">
<div class="col-md-4">
<form asp-action="Add">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="ServerName" class="control-label"></label>
<input asp-for="ServerName" class="form-control" />
<span asp-validation-for="ServerName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ServerUrl" class="control-label"></label>
<input asp-for="ServerUrl" class="form-control" />
<span asp-validation-for="ServerUrl" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ServerIndex" class="control-label"></label>
<input asp-for="ServerIndex" class="form-control" />
<span asp-validation-for="ServerIndex" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Username" class="control-label"></label>
<input asp-for="Username" class="form-control" />
<span asp-validation-for="Username" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Password" class="control-label"></label>
<input asp-for="Password" class="form-control" />
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="SID" class="control-label"></label>
<input asp-for="SID" class="form-control" />
<span asp-validation-for="SID" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" id="btnSubmit" class="btn btn-primary" />
</div>
</form>
</div>
</div>
After clicking the Submit button, it will submit the form data to the action method (please check your code to make sure the action attribute value is correct and the element's name value is correct).
the screenshot as below (if remove the SID element, it also transfers data to the Post action method):
If use JQuery code to submit the form data, code like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(function () {
$("#btnSubmit").click(function () {
event.preventDefault(); //prevent the submit button default event.
var server = {}; //create a object and get the entered data
server.ServerName = $("input[name='ServerName']").val();
server.ServerUrl = $("input[name='ServerUrl']").val();
server.ServerIndex = $("input[name='ServerIndex']").val();
server.Username = $("input[name='Username']").val();
server.Password = $("input[name='Password']").val();
server.SID = $("input[name='SID']").val();
$.ajax({
type: "POST",
url: "/Home/Add",
data: server,
success: function (data) {
console.log(data)
},
failure: function (response) {
console.log(response.responseText);
},
error: function (response) {
console.log(response.responseText);
}
});
});
});
</script>
The result like this (If removing the SID element, it still works well):
Upvotes: 1
Reputation: 940
Maybe you loss something.
If you are using https
not http
, your HttpPost client should disable SSL validation. Don't do this in production.... Just debugging!!
Like Postman, in Settings->General->SSL certificate verification->set Off
Your function to get HttpPost parameter with [FromBody]
attribute, like this:
[HttpPost]
public Server Add([FromBody] Server server)
{
return server;
}
Upvotes: 0
Reputation: 17
[HttpPost]
public Server Add([FromBody]Server server)
{
return _serverService.Add(server);
}
Have you tried using FromBody?
Upvotes: 1