0

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.

5
  • 3
    When people say "put business logic in the model," they aren't referring to the view model, they are referring to the M in MVC which is essentially whatever your controllers interact with - a service layer, API, domain model... it is a misleading way of putting it. Commented May 27, 2015 at 19:51
  • @AntP, so I should create another class (a Model) just for the business logic and call that method and pass the required values from the Controller? That doesn't sound like the best idea or I might be missing something. Commented May 27, 2015 at 19:57
  • Create a folder in your app called Business, create a class in that folder so it will now have a name space like YourApp.Business.YourClass. Now do your business login in that class and call it from your viewModel, or where ever you want to call it from, really. Commented May 27, 2015 at 20:00
  • Based on what you've posted, I would create an interface IPersonService (assuming that this is for information about people) and then an implementation called ThirdPartyApiPersonService and 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) in IPersonService. 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. Commented May 27, 2015 at 20:00
  • Keep your entity models clean (POCO). Use your controller to build a ViewModel you send to the View. Then in the controller get the stuff from EF, call the API, and populate the ViewModel. lostechies.com/jimmybogard/2009/06/30/how-we-do-mvc-view-models Commented May 27, 2015 at 20:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.