I have a simple CRUD application in MVC using EntityFramework. I need to use an external API after creating a new element, but I am not sure from where to do that.
I have read in multiple threads on this website and others that business logic should be in the Model and not in the Controller, however I am not sure how to get it to work.
This is what my Model looks like:
public int ID { get; set; }
private string _name;
[Required(ErrorMessage = "Required")]
public string Name
{
get { return _name; }
set
{
_name= value;
if (value != null)
{
this.doAPIStuff();
}
}
}
[Required(ErrorMessage = "Required")]
public string PersonalNumber { get; set; }
public void doAPIStuff()
{
// do stuff
}
As you can see I am trying to execute my doAPIStuff() method after adding data to the database, however when I do input something in the field Name I get an error The value '<value>' is invalid (where is what I have written in the text box).
Neither of these helped me solve my problem:
The value “(string)” is invalid
ASP.NET MVC Page Validation Fails (The value is invalid.)
DropDownList Value is always invalid in MVC 3
I have not touched the View - here is most of it:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Person</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PersonalNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PersonalNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PersonalNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
I did test the code from doAPIStuff() in my Controller, so I know for sure that it works, however I am not sure how to run it as shown above. Another option I was thinking of was creating a new model just for the doAPIStuff() method, however then I would have to pass the Name and PersonalNumber values to the Model, which seems like the wrong thing to do.
IPersonService(assuming that this is for information about people) and then an implementation calledThirdPartyApiPersonServiceand inject that into the controller - then you can decouple your web layer from the specific API you're calling and wrap your business logic up (if there is any beyond what's already in the third-party API) inIPersonService. Controller takes values from the view via the view model and passes them off to the service - the service doesn't care about the fact that it's being called from a web application.