2

I am working on an ASP.NET MVC web portal that requires users to accept Terms of Use when they first log in. After the user clicks to accept, I need to pass back a bool (AcceptedTerms) as true. This value exists in the DB and there is a repository value (PortalUser.AcceptedTermsAndConditions) that will be used to push or pull the value from the DB. In the controller,

[ChildActionOnly]
public ActionResult RenderLogIn()
{
    PortalUser portalUser = new PortalUser();
    LogInViewModels model = new LogInViewModels() { AcceptedTerms = portalUser.AcceptedTermsAndConditions };
    return PartialView("_LogIn", model);
}

Then in the model:

public class LogInViewModels
{
    public bool AcceptedTerms { get; set; }
}

And in the view:

@if (Model.AcceptedTerms == false)
{
    <script> 
        $("#TOUModal").modal('show');

        function validateTerms() {
            var valid = ($("#chkTOU").prop("checked"));
            $("#submitTerms").prop("disabled", !valid);
        }

        $("#chkTOU").change(validateTerms);

        $("#submitTerms").click(function () {
            @Model.AcceptedTerms == true;
        });
    </script>
}

Lastly, I've got a repository method to update the value in the DB:

public void UpdateAcceptedTermsAndConditions( int UserID, Boolean Accepted )
{
    VerifyConnectionString_LicensePortal();

    SqlDataSource sqlDataSource = new SqlDataSource( ConnectionString_LicensePortal, "" );

    String strSQLQuery = "UPDATE [dbo].[Users] SET [AcceptedTermsAndConditions] = @Accepted WHERE [UserId] = @UserID;";

    sqlDataSource.UpdateCommand = strSQLQuery;

    sqlDataSource.UpdateCommandType = SqlDataSourceCommandType.Text;

    sqlDataSource.CancelSelectOnNullParameter = false;

    sqlDataSource.UpdateParameters.Clear();

    sqlDataSource.UpdateParameters.Add( "Accepted", DbType.Boolean, Accepted.ToString() );
    sqlDataSource.UpdateParameters.Add( "UserID",   DbType.Int32,   UserID.ToString()   );

    sqlDataSource.Update();
}

How do I tie all of these things together so that upon login, I'm properly pulling in the DB value, and when the terms are accepted, the values gets updated in the DB?

elmer007
  • 1,412
  • 14
  • 27
pixelstars
  • 21
  • 4

1 Answers1

0

Start by adding the new property to LogInViewModels: UserID and bind it in RenderLogIn(). Then you have to make next method in controller (one, in which you store RenderLogIn() method). I see you want to use javascript to send Model.AcceptedTerms data instead of submit form, so you have to create AJAX call to newly created controller's method.

LogInViewModels new property:

public int UserId { get; set; }

Modified RenderLogIn() method:

[ChildActionOnly]
public ActionResult RenderLogIn()
{
    PortalUser portalUser = new PortalUser();
    LogInViewModels model = new LogInViewModels() { 
        AcceptedTerms = portalUser.AcceptedTermsAndConditions, 
        UserId = portalUser.Id }; // or you have to get this Id from other DB source
    return PartialView("_LogIn", model);
}

Controller's method:

[HttpPost]
public ActionResult AcceptTermsAndConditions(int userId, Boolean accepted)
{
    UpdateAcceptedTermsAndConditions(userId, accepted);
    return PartialView("YourView");
}

Javascript ajax call in view:

var model = @Html.Raw(Json.Encode(Model));
$("#submitTerms").click(function () {
           model.AcceptedTerms = true;
           $.ajax({  
                    type: "POST",  
                    url: '@Url.Action("AcceptTermsAndConditions", "YourController")',  
                    contentType: "application/json; charset=utf-8",  
                    data: JSON.stringify(model),  
                    dataType: "html"
                });
});

For more about transforming model to json object: How do I use Razor values in a javascript function?