How do i update prices with view for admin ? My rates changes overtime. how do i allow the admin to update...












-1















Here are my codes, im using asp.net Web applications MVC



Basically just need allow admin/superuser to be able to update the rates for membership 'in those 2 textboxes names lifetime and yearlyrates and it will display in "Prices" in a non-admin view when clicked on the duration for membership renewal. Would be great if someone can help me with these codes.



This is the view that allows admin to update the prices for yearly and lifetime membership rates



 @model RenewMember
@{
ViewData["Title"] = "Membership Renewal";
Layout = "~/Views/Shared/_Layout.cshtml";

string msg = ViewData["msg"] as string;

var Duration = new
{
new { value=1, text="1 year"},
new { value=2, text="2 years"},
new { value=3, text="3 years"},
new { value=4, text="Life-time"}
};

@section ScriptSection {
<script>
function DisplayPrice() {
var price1yearmember = 100;
var rate = 1;
var subtotal = 0;
if (selectedCurrency == "USD") {
subtotal = price1yearmember * rateUSD;
}
else if (selectedCurrency == "MYR") {
subtotal = price1yearmember * rateMYR;
}
else if (selectedCurrency == "SGD") {
subtotal = price1yearmember * rate;
}
$("#txtPrice").val(subtotal.toFixed(2));
}

</script>
}
}

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Contact Us</title>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<link href="~/lib/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="~/lib/moment/min/moment.min.js"></script>
<script src="~/lib/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
<script type='text/javascript' src='https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&ver=2.0'></script>

<style>
<style > .input-group-addon {
cursor: pointer;
}

#BsDateTimePicker {
cursor: pointer;
}
</style>
<script type="text/javascript">
$(function () {
$('#BsDateTimePicker').datetimepicker({
format: 'YYYY-MM-DD'
});
});
</script>
<script src="~/lib/jquery-validation/src/core.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<meta charset="utf-8" />
<title>Contact Us</title>
<style type="text/css">
#head {
font-family: 'Times New Roman', Times, serif;
font-style: oblique;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
font-weight: 900;
}

#late {
font-family: 'Times New Roman', Times, serif;
font-style: italic;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
}

.field-validation-error {
font-weight: bold;
color: red;
/*background-color: yellow;*/
font-family: 'Times New Roman', Times, serif;
font-style: unset;
font-size: medium;
}

.validation-summary-errors {
border: 2px dashed red;
color: red;
/*background-color: yellow;*/
font-weight: bold;
margin: 12px;
}
</style>
</head>

<form class="form-horizontal" asp-action="RenewMember">

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{

<div class="form-group">
<label input id="txtYearRate" class="control-label col-sm-3" asp-for="YearlyRate">Rates per year : </label>
<div class="col-sm-5">
<input asp-for="YearlyRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="YearlyRate" class="text-danger"></span>
</div>
</div>



<div class="form-group">
<label input id="txtLifeRate" class="control-label col-sm-3" asp-for="LifeRate">Rates for Lifetime membership : </label>
<div class="col-sm-5">
<input asp-for="LifeRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="LifeRate" class="text-danger"></span>
</div>
</div>

}
else
{
<div class="form-group">
<label class="control-label col-sm-3" asp-for="Email">Email : </label>
<div class="col-sm-5">
<input asp-for="Email" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="control-label col-sm-3" asp-for="RenewDuration">Duration : </label>
<div class="col-sm-4">
@foreach (var freq in Duration)
{
<input type="radio" asp-for="RenewDuration" value="@freq.value" /> @freq.text
}
</div>
<div class="has-error">
<span asp-validation-for="RenewDuration" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label">Price: </label>
<div class="col-sm-6" style="padding-right:15px;">
<input id="txtPrice" class="form-control" value="-" readonly disabled style="background-color:#C0C0C0" />
</div>
<div class="col-sm-1" style="padding-left:0">
@{
Html.RenderPartial("_CurrencySelector");
}
</div>
</div>

}

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</div>
}
else
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Renew" />
</div>
</div>

}


</form>


This is my model code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace PSS.Models
{
public class RenewMember
{


[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Required Field")]
public string Email { get; set; }

[Range(1, 4, ErrorMessage = "Please select a Duration for renewal")]
public int RenewDuration { get; set; }

[Required(ErrorMessage = "Required Field")]
public int YearlyRate { get; set; }

[Required(ErrorMessage = "Required Field")]
public int LifeRate { get; set; }

public string Currency { get; set; }
public float CurrencyRate { get; set; }
}
}


And this will be my controller which has nothing much in it yet



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PSS.Models;
using System.Security.Claims;
using System.Data;
using Microsoft.EntityFrameworkCore;
using System.Text;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using MimeKit;
using static System.Net.Mime.MediaTypeNames;
using MailKit.Net.Smtp;
using System.Linq.Dynamic;
using Microsoft.AspNetCore.Authorization;
using System.Dynamic;

namespace PSS.Controllers
{
public class RenewMemberController : Controller
{
private AppDbContext _dbContext;

public RenewMemberController(AppDbContext dbContext)
{
_dbContext = dbContext;
}

public IActionResult Index()
{
// DbSet<RenewMember> dbs = _dbContext.RenewMember;
//var lstType =
// dbs.ToList<RenewMember>()
// .OrderBy(p => p.TypeName)
// .Select(
// p =>
// {
// dynamic d = new ExpandoObject();
// d.value = p.MemberTypeId;
//d.text = p.TypeName;
//return d;
// }
// )
//.ToList<dynamic>();
//ViewData["currency"] = lstCurrency;

return View();
}
}
}


This is what i tried



    [HttpPost]
public IActionResult UpdateRate(RenewMember rate)
{
if (ModelState.IsValid)
{
DbSet<RenewMember> dbs = _dbContext.RenewMember;
RenewMember nrate = dbs.Where(m => m.Id == rate.Id).FirstOrDefault();

if (rate != null)
{
nrate.YearlyRate = rate.YearlyRate;
nrate.LifeRate = rate.LifeRate;

string msg = "";
if (_dbContext.SaveChanges() == 1)
msg = String.Format("Rates info updated!");
TempData["Msg"] = msg;
}
else
{
TempData["Msg"] = "Rate not found!";
return RedirectToAction("Index");
}
}
else
{
TempData["Msg"] = "Invalid information entered";
}
return RedirectToAction("Index");
}


When i click "Save" button it shows localhost page not found, it redirects me to "http://localhost:49228/renewmember/RenewMember" which does not exist. My page is suppose to be http://localhost:49228/renewmember/Index. However my database is still not updated.










share|improve this question

























  • Are you just asking how to save data to and read data from a database in ASP.NET MVC? Any introductory tutorial which includes using a database would cover that. Have you tried following a few tutorials? Have you made any attempt? What have you tried and what specifically isn't working as expected?

    – David
    Nov 25 '18 at 13:31











  • this is what i tried

    – Brendon C
    Nov 25 '18 at 14:58











  • it doesnt have to be placed into database, it just need to retrieve the value that admin has entered be it databse or not. because the rates changes overtime and the admin can change the rates

    – Brendon C
    Nov 25 '18 at 15:00













  • What is this typo: <style> <style > .input-group-addon {

    – Mark Schultheiss
    Nov 25 '18 at 15:03






  • 1





    Please update the question to show your attempt and to indicate what specifically isn’t working as expected.

    – David
    Nov 25 '18 at 15:05
















-1















Here are my codes, im using asp.net Web applications MVC



Basically just need allow admin/superuser to be able to update the rates for membership 'in those 2 textboxes names lifetime and yearlyrates and it will display in "Prices" in a non-admin view when clicked on the duration for membership renewal. Would be great if someone can help me with these codes.



This is the view that allows admin to update the prices for yearly and lifetime membership rates



 @model RenewMember
@{
ViewData["Title"] = "Membership Renewal";
Layout = "~/Views/Shared/_Layout.cshtml";

string msg = ViewData["msg"] as string;

var Duration = new
{
new { value=1, text="1 year"},
new { value=2, text="2 years"},
new { value=3, text="3 years"},
new { value=4, text="Life-time"}
};

@section ScriptSection {
<script>
function DisplayPrice() {
var price1yearmember = 100;
var rate = 1;
var subtotal = 0;
if (selectedCurrency == "USD") {
subtotal = price1yearmember * rateUSD;
}
else if (selectedCurrency == "MYR") {
subtotal = price1yearmember * rateMYR;
}
else if (selectedCurrency == "SGD") {
subtotal = price1yearmember * rate;
}
$("#txtPrice").val(subtotal.toFixed(2));
}

</script>
}
}

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Contact Us</title>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<link href="~/lib/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="~/lib/moment/min/moment.min.js"></script>
<script src="~/lib/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
<script type='text/javascript' src='https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&ver=2.0'></script>

<style>
<style > .input-group-addon {
cursor: pointer;
}

#BsDateTimePicker {
cursor: pointer;
}
</style>
<script type="text/javascript">
$(function () {
$('#BsDateTimePicker').datetimepicker({
format: 'YYYY-MM-DD'
});
});
</script>
<script src="~/lib/jquery-validation/src/core.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<meta charset="utf-8" />
<title>Contact Us</title>
<style type="text/css">
#head {
font-family: 'Times New Roman', Times, serif;
font-style: oblique;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
font-weight: 900;
}

#late {
font-family: 'Times New Roman', Times, serif;
font-style: italic;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
}

.field-validation-error {
font-weight: bold;
color: red;
/*background-color: yellow;*/
font-family: 'Times New Roman', Times, serif;
font-style: unset;
font-size: medium;
}

.validation-summary-errors {
border: 2px dashed red;
color: red;
/*background-color: yellow;*/
font-weight: bold;
margin: 12px;
}
</style>
</head>

<form class="form-horizontal" asp-action="RenewMember">

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{

<div class="form-group">
<label input id="txtYearRate" class="control-label col-sm-3" asp-for="YearlyRate">Rates per year : </label>
<div class="col-sm-5">
<input asp-for="YearlyRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="YearlyRate" class="text-danger"></span>
</div>
</div>



<div class="form-group">
<label input id="txtLifeRate" class="control-label col-sm-3" asp-for="LifeRate">Rates for Lifetime membership : </label>
<div class="col-sm-5">
<input asp-for="LifeRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="LifeRate" class="text-danger"></span>
</div>
</div>

}
else
{
<div class="form-group">
<label class="control-label col-sm-3" asp-for="Email">Email : </label>
<div class="col-sm-5">
<input asp-for="Email" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="control-label col-sm-3" asp-for="RenewDuration">Duration : </label>
<div class="col-sm-4">
@foreach (var freq in Duration)
{
<input type="radio" asp-for="RenewDuration" value="@freq.value" /> @freq.text
}
</div>
<div class="has-error">
<span asp-validation-for="RenewDuration" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label">Price: </label>
<div class="col-sm-6" style="padding-right:15px;">
<input id="txtPrice" class="form-control" value="-" readonly disabled style="background-color:#C0C0C0" />
</div>
<div class="col-sm-1" style="padding-left:0">
@{
Html.RenderPartial("_CurrencySelector");
}
</div>
</div>

}

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</div>
}
else
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Renew" />
</div>
</div>

}


</form>


This is my model code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace PSS.Models
{
public class RenewMember
{


[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Required Field")]
public string Email { get; set; }

[Range(1, 4, ErrorMessage = "Please select a Duration for renewal")]
public int RenewDuration { get; set; }

[Required(ErrorMessage = "Required Field")]
public int YearlyRate { get; set; }

[Required(ErrorMessage = "Required Field")]
public int LifeRate { get; set; }

public string Currency { get; set; }
public float CurrencyRate { get; set; }
}
}


And this will be my controller which has nothing much in it yet



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PSS.Models;
using System.Security.Claims;
using System.Data;
using Microsoft.EntityFrameworkCore;
using System.Text;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using MimeKit;
using static System.Net.Mime.MediaTypeNames;
using MailKit.Net.Smtp;
using System.Linq.Dynamic;
using Microsoft.AspNetCore.Authorization;
using System.Dynamic;

namespace PSS.Controllers
{
public class RenewMemberController : Controller
{
private AppDbContext _dbContext;

public RenewMemberController(AppDbContext dbContext)
{
_dbContext = dbContext;
}

public IActionResult Index()
{
// DbSet<RenewMember> dbs = _dbContext.RenewMember;
//var lstType =
// dbs.ToList<RenewMember>()
// .OrderBy(p => p.TypeName)
// .Select(
// p =>
// {
// dynamic d = new ExpandoObject();
// d.value = p.MemberTypeId;
//d.text = p.TypeName;
//return d;
// }
// )
//.ToList<dynamic>();
//ViewData["currency"] = lstCurrency;

return View();
}
}
}


This is what i tried



    [HttpPost]
public IActionResult UpdateRate(RenewMember rate)
{
if (ModelState.IsValid)
{
DbSet<RenewMember> dbs = _dbContext.RenewMember;
RenewMember nrate = dbs.Where(m => m.Id == rate.Id).FirstOrDefault();

if (rate != null)
{
nrate.YearlyRate = rate.YearlyRate;
nrate.LifeRate = rate.LifeRate;

string msg = "";
if (_dbContext.SaveChanges() == 1)
msg = String.Format("Rates info updated!");
TempData["Msg"] = msg;
}
else
{
TempData["Msg"] = "Rate not found!";
return RedirectToAction("Index");
}
}
else
{
TempData["Msg"] = "Invalid information entered";
}
return RedirectToAction("Index");
}


When i click "Save" button it shows localhost page not found, it redirects me to "http://localhost:49228/renewmember/RenewMember" which does not exist. My page is suppose to be http://localhost:49228/renewmember/Index. However my database is still not updated.










share|improve this question

























  • Are you just asking how to save data to and read data from a database in ASP.NET MVC? Any introductory tutorial which includes using a database would cover that. Have you tried following a few tutorials? Have you made any attempt? What have you tried and what specifically isn't working as expected?

    – David
    Nov 25 '18 at 13:31











  • this is what i tried

    – Brendon C
    Nov 25 '18 at 14:58











  • it doesnt have to be placed into database, it just need to retrieve the value that admin has entered be it databse or not. because the rates changes overtime and the admin can change the rates

    – Brendon C
    Nov 25 '18 at 15:00













  • What is this typo: <style> <style > .input-group-addon {

    – Mark Schultheiss
    Nov 25 '18 at 15:03






  • 1





    Please update the question to show your attempt and to indicate what specifically isn’t working as expected.

    – David
    Nov 25 '18 at 15:05














-1












-1








-1








Here are my codes, im using asp.net Web applications MVC



Basically just need allow admin/superuser to be able to update the rates for membership 'in those 2 textboxes names lifetime and yearlyrates and it will display in "Prices" in a non-admin view when clicked on the duration for membership renewal. Would be great if someone can help me with these codes.



This is the view that allows admin to update the prices for yearly and lifetime membership rates



 @model RenewMember
@{
ViewData["Title"] = "Membership Renewal";
Layout = "~/Views/Shared/_Layout.cshtml";

string msg = ViewData["msg"] as string;

var Duration = new
{
new { value=1, text="1 year"},
new { value=2, text="2 years"},
new { value=3, text="3 years"},
new { value=4, text="Life-time"}
};

@section ScriptSection {
<script>
function DisplayPrice() {
var price1yearmember = 100;
var rate = 1;
var subtotal = 0;
if (selectedCurrency == "USD") {
subtotal = price1yearmember * rateUSD;
}
else if (selectedCurrency == "MYR") {
subtotal = price1yearmember * rateMYR;
}
else if (selectedCurrency == "SGD") {
subtotal = price1yearmember * rate;
}
$("#txtPrice").val(subtotal.toFixed(2));
}

</script>
}
}

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Contact Us</title>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<link href="~/lib/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="~/lib/moment/min/moment.min.js"></script>
<script src="~/lib/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
<script type='text/javascript' src='https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&ver=2.0'></script>

<style>
<style > .input-group-addon {
cursor: pointer;
}

#BsDateTimePicker {
cursor: pointer;
}
</style>
<script type="text/javascript">
$(function () {
$('#BsDateTimePicker').datetimepicker({
format: 'YYYY-MM-DD'
});
});
</script>
<script src="~/lib/jquery-validation/src/core.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<meta charset="utf-8" />
<title>Contact Us</title>
<style type="text/css">
#head {
font-family: 'Times New Roman', Times, serif;
font-style: oblique;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
font-weight: 900;
}

#late {
font-family: 'Times New Roman', Times, serif;
font-style: italic;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
}

.field-validation-error {
font-weight: bold;
color: red;
/*background-color: yellow;*/
font-family: 'Times New Roman', Times, serif;
font-style: unset;
font-size: medium;
}

.validation-summary-errors {
border: 2px dashed red;
color: red;
/*background-color: yellow;*/
font-weight: bold;
margin: 12px;
}
</style>
</head>

<form class="form-horizontal" asp-action="RenewMember">

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{

<div class="form-group">
<label input id="txtYearRate" class="control-label col-sm-3" asp-for="YearlyRate">Rates per year : </label>
<div class="col-sm-5">
<input asp-for="YearlyRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="YearlyRate" class="text-danger"></span>
</div>
</div>



<div class="form-group">
<label input id="txtLifeRate" class="control-label col-sm-3" asp-for="LifeRate">Rates for Lifetime membership : </label>
<div class="col-sm-5">
<input asp-for="LifeRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="LifeRate" class="text-danger"></span>
</div>
</div>

}
else
{
<div class="form-group">
<label class="control-label col-sm-3" asp-for="Email">Email : </label>
<div class="col-sm-5">
<input asp-for="Email" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="control-label col-sm-3" asp-for="RenewDuration">Duration : </label>
<div class="col-sm-4">
@foreach (var freq in Duration)
{
<input type="radio" asp-for="RenewDuration" value="@freq.value" /> @freq.text
}
</div>
<div class="has-error">
<span asp-validation-for="RenewDuration" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label">Price: </label>
<div class="col-sm-6" style="padding-right:15px;">
<input id="txtPrice" class="form-control" value="-" readonly disabled style="background-color:#C0C0C0" />
</div>
<div class="col-sm-1" style="padding-left:0">
@{
Html.RenderPartial("_CurrencySelector");
}
</div>
</div>

}

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</div>
}
else
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Renew" />
</div>
</div>

}


</form>


This is my model code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace PSS.Models
{
public class RenewMember
{


[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Required Field")]
public string Email { get; set; }

[Range(1, 4, ErrorMessage = "Please select a Duration for renewal")]
public int RenewDuration { get; set; }

[Required(ErrorMessage = "Required Field")]
public int YearlyRate { get; set; }

[Required(ErrorMessage = "Required Field")]
public int LifeRate { get; set; }

public string Currency { get; set; }
public float CurrencyRate { get; set; }
}
}


And this will be my controller which has nothing much in it yet



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PSS.Models;
using System.Security.Claims;
using System.Data;
using Microsoft.EntityFrameworkCore;
using System.Text;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using MimeKit;
using static System.Net.Mime.MediaTypeNames;
using MailKit.Net.Smtp;
using System.Linq.Dynamic;
using Microsoft.AspNetCore.Authorization;
using System.Dynamic;

namespace PSS.Controllers
{
public class RenewMemberController : Controller
{
private AppDbContext _dbContext;

public RenewMemberController(AppDbContext dbContext)
{
_dbContext = dbContext;
}

public IActionResult Index()
{
// DbSet<RenewMember> dbs = _dbContext.RenewMember;
//var lstType =
// dbs.ToList<RenewMember>()
// .OrderBy(p => p.TypeName)
// .Select(
// p =>
// {
// dynamic d = new ExpandoObject();
// d.value = p.MemberTypeId;
//d.text = p.TypeName;
//return d;
// }
// )
//.ToList<dynamic>();
//ViewData["currency"] = lstCurrency;

return View();
}
}
}


This is what i tried



    [HttpPost]
public IActionResult UpdateRate(RenewMember rate)
{
if (ModelState.IsValid)
{
DbSet<RenewMember> dbs = _dbContext.RenewMember;
RenewMember nrate = dbs.Where(m => m.Id == rate.Id).FirstOrDefault();

if (rate != null)
{
nrate.YearlyRate = rate.YearlyRate;
nrate.LifeRate = rate.LifeRate;

string msg = "";
if (_dbContext.SaveChanges() == 1)
msg = String.Format("Rates info updated!");
TempData["Msg"] = msg;
}
else
{
TempData["Msg"] = "Rate not found!";
return RedirectToAction("Index");
}
}
else
{
TempData["Msg"] = "Invalid information entered";
}
return RedirectToAction("Index");
}


When i click "Save" button it shows localhost page not found, it redirects me to "http://localhost:49228/renewmember/RenewMember" which does not exist. My page is suppose to be http://localhost:49228/renewmember/Index. However my database is still not updated.










share|improve this question
















Here are my codes, im using asp.net Web applications MVC



Basically just need allow admin/superuser to be able to update the rates for membership 'in those 2 textboxes names lifetime and yearlyrates and it will display in "Prices" in a non-admin view when clicked on the duration for membership renewal. Would be great if someone can help me with these codes.



This is the view that allows admin to update the prices for yearly and lifetime membership rates



 @model RenewMember
@{
ViewData["Title"] = "Membership Renewal";
Layout = "~/Views/Shared/_Layout.cshtml";

string msg = ViewData["msg"] as string;

var Duration = new
{
new { value=1, text="1 year"},
new { value=2, text="2 years"},
new { value=3, text="3 years"},
new { value=4, text="Life-time"}
};

@section ScriptSection {
<script>
function DisplayPrice() {
var price1yearmember = 100;
var rate = 1;
var subtotal = 0;
if (selectedCurrency == "USD") {
subtotal = price1yearmember * rateUSD;
}
else if (selectedCurrency == "MYR") {
subtotal = price1yearmember * rateMYR;
}
else if (selectedCurrency == "SGD") {
subtotal = price1yearmember * rate;
}
$("#txtPrice").val(subtotal.toFixed(2));
}

</script>
}
}

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Contact Us</title>
<link href="~/lib/bootstrap/dist/css/bootstrap.css" rel="stylesheet" />
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.min.js"></script>
<link href="~/lib/eonasdan-bootstrap-datetimepicker/build/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />
<script src="~/lib/moment/min/moment.min.js"></script>
<script src="~/lib/eonasdan-bootstrap-datetimepicker/build/js/bootstrap-datetimepicker.min.js"></script>
<script type='text/javascript' src='https://www.google.com/recaptcha/api.js?onload=recaptchaCallback&render=explicit&ver=2.0'></script>

<style>
<style > .input-group-addon {
cursor: pointer;
}

#BsDateTimePicker {
cursor: pointer;
}
</style>
<script type="text/javascript">
$(function () {
$('#BsDateTimePicker').datetimepicker({
format: 'YYYY-MM-DD'
});
});
</script>
<script src="~/lib/jquery-validation/src/core.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<meta charset="utf-8" />
<title>Contact Us</title>
<style type="text/css">
#head {
font-family: 'Times New Roman', Times, serif;
font-style: oblique;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
font-weight: 900;
}

#late {
font-family: 'Times New Roman', Times, serif;
font-style: italic;
font-size: larger;
text-align: center;
color: black;
will-change: contents;
}

.field-validation-error {
font-weight: bold;
color: red;
/*background-color: yellow;*/
font-family: 'Times New Roman', Times, serif;
font-style: unset;
font-size: medium;
}

.validation-summary-errors {
border: 2px dashed red;
color: red;
/*background-color: yellow;*/
font-weight: bold;
margin: 12px;
}
</style>
</head>

<form class="form-horizontal" asp-action="RenewMember">

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{

<div class="form-group">
<label input id="txtYearRate" class="control-label col-sm-3" asp-for="YearlyRate">Rates per year : </label>
<div class="col-sm-5">
<input asp-for="YearlyRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="YearlyRate" class="text-danger"></span>
</div>
</div>



<div class="form-group">
<label input id="txtLifeRate" class="control-label col-sm-3" asp-for="LifeRate">Rates for Lifetime membership : </label>
<div class="col-sm-5">
<input asp-for="LifeRate" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="LifeRate" class="text-danger"></span>
</div>
</div>

}
else
{
<div class="form-group">
<label class="control-label col-sm-3" asp-for="Email">Email : </label>
<div class="col-sm-5">
<input asp-for="Email" class="form-control" />
</div>
<div class="has-error">
<span asp-validation-for="Email" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="control-label col-sm-3" asp-for="RenewDuration">Duration : </label>
<div class="col-sm-4">
@foreach (var freq in Duration)
{
<input type="radio" asp-for="RenewDuration" value="@freq.value" /> @freq.text
}
</div>
<div class="has-error">
<span asp-validation-for="RenewDuration" class="text-danger"></span>
</div>
</div>

<div class="form-group">
<label class="col-sm-3 control-label">Price: </label>
<div class="col-sm-6" style="padding-right:15px;">
<input id="txtPrice" class="form-control" value="-" readonly disabled style="background-color:#C0C0C0" />
</div>
<div class="col-sm-1" style="padding-left:0">
@{
Html.RenderPartial("_CurrencySelector");
}
</div>
</div>

}

@if (User.IsInRole("Admin") || User.IsInRole("SuperUser"))
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Save" />
</div>
</div>
}
else
{
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
<input type="submit" class="btn btn-primary" value="Renew" />
</div>
</div>

}


</form>


This is my model code



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.ComponentModel.DataAnnotations;

namespace PSS.Models
{
public class RenewMember
{


[DataType(DataType.EmailAddress)]
[Required(ErrorMessage = "Required Field")]
public string Email { get; set; }

[Range(1, 4, ErrorMessage = "Please select a Duration for renewal")]
public int RenewDuration { get; set; }

[Required(ErrorMessage = "Required Field")]
public int YearlyRate { get; set; }

[Required(ErrorMessage = "Required Field")]
public int LifeRate { get; set; }

public string Currency { get; set; }
public float CurrencyRate { get; set; }
}
}


And this will be my controller which has nothing much in it yet



using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using PSS.Models;
using System.Security.Claims;
using System.Data;
using Microsoft.EntityFrameworkCore;
using System.Text;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.Web;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using MimeKit;
using static System.Net.Mime.MediaTypeNames;
using MailKit.Net.Smtp;
using System.Linq.Dynamic;
using Microsoft.AspNetCore.Authorization;
using System.Dynamic;

namespace PSS.Controllers
{
public class RenewMemberController : Controller
{
private AppDbContext _dbContext;

public RenewMemberController(AppDbContext dbContext)
{
_dbContext = dbContext;
}

public IActionResult Index()
{
// DbSet<RenewMember> dbs = _dbContext.RenewMember;
//var lstType =
// dbs.ToList<RenewMember>()
// .OrderBy(p => p.TypeName)
// .Select(
// p =>
// {
// dynamic d = new ExpandoObject();
// d.value = p.MemberTypeId;
//d.text = p.TypeName;
//return d;
// }
// )
//.ToList<dynamic>();
//ViewData["currency"] = lstCurrency;

return View();
}
}
}


This is what i tried



    [HttpPost]
public IActionResult UpdateRate(RenewMember rate)
{
if (ModelState.IsValid)
{
DbSet<RenewMember> dbs = _dbContext.RenewMember;
RenewMember nrate = dbs.Where(m => m.Id == rate.Id).FirstOrDefault();

if (rate != null)
{
nrate.YearlyRate = rate.YearlyRate;
nrate.LifeRate = rate.LifeRate;

string msg = "";
if (_dbContext.SaveChanges() == 1)
msg = String.Format("Rates info updated!");
TempData["Msg"] = msg;
}
else
{
TempData["Msg"] = "Rate not found!";
return RedirectToAction("Index");
}
}
else
{
TempData["Msg"] = "Invalid information entered";
}
return RedirectToAction("Index");
}


When i click "Save" button it shows localhost page not found, it redirects me to "http://localhost:49228/renewmember/RenewMember" which does not exist. My page is suppose to be http://localhost:49228/renewmember/Index. However my database is still not updated.







asp.net-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 15:40







Brendon C

















asked Nov 25 '18 at 13:26









Brendon CBrendon C

11




11













  • Are you just asking how to save data to and read data from a database in ASP.NET MVC? Any introductory tutorial which includes using a database would cover that. Have you tried following a few tutorials? Have you made any attempt? What have you tried and what specifically isn't working as expected?

    – David
    Nov 25 '18 at 13:31











  • this is what i tried

    – Brendon C
    Nov 25 '18 at 14:58











  • it doesnt have to be placed into database, it just need to retrieve the value that admin has entered be it databse or not. because the rates changes overtime and the admin can change the rates

    – Brendon C
    Nov 25 '18 at 15:00













  • What is this typo: <style> <style > .input-group-addon {

    – Mark Schultheiss
    Nov 25 '18 at 15:03






  • 1





    Please update the question to show your attempt and to indicate what specifically isn’t working as expected.

    – David
    Nov 25 '18 at 15:05



















  • Are you just asking how to save data to and read data from a database in ASP.NET MVC? Any introductory tutorial which includes using a database would cover that. Have you tried following a few tutorials? Have you made any attempt? What have you tried and what specifically isn't working as expected?

    – David
    Nov 25 '18 at 13:31











  • this is what i tried

    – Brendon C
    Nov 25 '18 at 14:58











  • it doesnt have to be placed into database, it just need to retrieve the value that admin has entered be it databse or not. because the rates changes overtime and the admin can change the rates

    – Brendon C
    Nov 25 '18 at 15:00













  • What is this typo: <style> <style > .input-group-addon {

    – Mark Schultheiss
    Nov 25 '18 at 15:03






  • 1





    Please update the question to show your attempt and to indicate what specifically isn’t working as expected.

    – David
    Nov 25 '18 at 15:05

















Are you just asking how to save data to and read data from a database in ASP.NET MVC? Any introductory tutorial which includes using a database would cover that. Have you tried following a few tutorials? Have you made any attempt? What have you tried and what specifically isn't working as expected?

– David
Nov 25 '18 at 13:31





Are you just asking how to save data to and read data from a database in ASP.NET MVC? Any introductory tutorial which includes using a database would cover that. Have you tried following a few tutorials? Have you made any attempt? What have you tried and what specifically isn't working as expected?

– David
Nov 25 '18 at 13:31













this is what i tried

– Brendon C
Nov 25 '18 at 14:58





this is what i tried

– Brendon C
Nov 25 '18 at 14:58













it doesnt have to be placed into database, it just need to retrieve the value that admin has entered be it databse or not. because the rates changes overtime and the admin can change the rates

– Brendon C
Nov 25 '18 at 15:00







it doesnt have to be placed into database, it just need to retrieve the value that admin has entered be it databse or not. because the rates changes overtime and the admin can change the rates

– Brendon C
Nov 25 '18 at 15:00















What is this typo: <style> <style > .input-group-addon {

– Mark Schultheiss
Nov 25 '18 at 15:03





What is this typo: <style> <style > .input-group-addon {

– Mark Schultheiss
Nov 25 '18 at 15:03




1




1





Please update the question to show your attempt and to indicate what specifically isn’t working as expected.

– David
Nov 25 '18 at 15:05





Please update the question to show your attempt and to indicate what specifically isn’t working as expected.

– David
Nov 25 '18 at 15:05












1 Answer
1






active

oldest

votes


















0














Just to answer the question, you have it pointing to RenewMember



<form class="form-horizontal" asp-action="RenewMember">


You might consider different controller methods:



<form class="form-horizontal" asp-action="UpdateAdminMember">


and a separate view with different controller method:



<form class="form-horizontal" asp-action="SaveNewMember">


OR use some helpers



@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
{


As for your example for admin (and another partial view for the non-admin view)



@using (Html.BeginForm("UpdateAdminMember", "RenewMember", FormMethod.Post))
{
@Html.LabelFor(m => m.YearlyRate)
@Html.TextBoxFor(m => m.YearlyRate)
...
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467945%2fhow-do-i-update-prices-with-view-for-admin-my-rates-changes-overtime-how-do-i%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Just to answer the question, you have it pointing to RenewMember



    <form class="form-horizontal" asp-action="RenewMember">


    You might consider different controller methods:



    <form class="form-horizontal" asp-action="UpdateAdminMember">


    and a separate view with different controller method:



    <form class="form-horizontal" asp-action="SaveNewMember">


    OR use some helpers



    @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
    {


    As for your example for admin (and another partial view for the non-admin view)



    @using (Html.BeginForm("UpdateAdminMember", "RenewMember", FormMethod.Post))
    {
    @Html.LabelFor(m => m.YearlyRate)
    @Html.TextBoxFor(m => m.YearlyRate)
    ...
    }





    share|improve this answer




























      0














      Just to answer the question, you have it pointing to RenewMember



      <form class="form-horizontal" asp-action="RenewMember">


      You might consider different controller methods:



      <form class="form-horizontal" asp-action="UpdateAdminMember">


      and a separate view with different controller method:



      <form class="form-horizontal" asp-action="SaveNewMember">


      OR use some helpers



      @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
      {


      As for your example for admin (and another partial view for the non-admin view)



      @using (Html.BeginForm("UpdateAdminMember", "RenewMember", FormMethod.Post))
      {
      @Html.LabelFor(m => m.YearlyRate)
      @Html.TextBoxFor(m => m.YearlyRate)
      ...
      }





      share|improve this answer


























        0












        0








        0







        Just to answer the question, you have it pointing to RenewMember



        <form class="form-horizontal" asp-action="RenewMember">


        You might consider different controller methods:



        <form class="form-horizontal" asp-action="UpdateAdminMember">


        and a separate view with different controller method:



        <form class="form-horizontal" asp-action="SaveNewMember">


        OR use some helpers



        @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
        {


        As for your example for admin (and another partial view for the non-admin view)



        @using (Html.BeginForm("UpdateAdminMember", "RenewMember", FormMethod.Post))
        {
        @Html.LabelFor(m => m.YearlyRate)
        @Html.TextBoxFor(m => m.YearlyRate)
        ...
        }





        share|improve this answer













        Just to answer the question, you have it pointing to RenewMember



        <form class="form-horizontal" asp-action="RenewMember">


        You might consider different controller methods:



        <form class="form-horizontal" asp-action="UpdateAdminMember">


        and a separate view with different controller method:



        <form class="form-horizontal" asp-action="SaveNewMember">


        OR use some helpers



        @using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))
        {


        As for your example for admin (and another partial view for the non-admin view)



        @using (Html.BeginForm("UpdateAdminMember", "RenewMember", FormMethod.Post))
        {
        @Html.LabelFor(m => m.YearlyRate)
        @Html.TextBoxFor(m => m.YearlyRate)
        ...
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 16:30









        Mark SchultheissMark Schultheiss

        24k85081




        24k85081






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53467945%2fhow-do-i-update-prices-with-view-for-admin-my-rates-changes-overtime-how-do-i%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks