0

So I have a Customers model which needs to have a one to zero-or-one relationship with AspNetUsers. The way I want this to work is for a user to register. Then, if they want to, go to example.com/Customers/Create and create a single Customers record which they can edit or delete. A user can only have one or zero Customer records.

They problem is that when a user has created a Customer, the Customer view gives the user the option to create another customer and if he does, an error occurs.

How do I prevent this? Users should automatically not be given the option to create a Customer if they've created one already like here :

I also have to prevent this server side.

Model:

public class Customer
{
    public int CustomerID { get; set; }
    public virtual ApplicationUser ApplicationUser { get; set; }

    public string Name { get; set; }
}

OnModelCreating:

        modelBuilder.Entity<ApplicationUser>()
        .HasOptional(m => m.Customer)
        .WithRequired(m => m.ApplicationUser)
        .Map(p => p.MapKey("UserId"))

Create method on controller (POST)

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "CustomerID,Name")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                UserManager<ApplicationUser> UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
                var CurrentUser = UserManager.FindById(User.Identity.GetUserId());
                customer.ApplicationUser = CurrentUser;
                db.Customers.Add(customer);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(customer);
        }

What should I change in my Index view so that isn't a 'Create new' option when a user has created a Customer, and in the Create view, stop anyone from creating anymore.

Index:

@model IEnumerable<WebApplication8.Models.Customer>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th></th>
    </tr>
@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.CustomerID }) |
            @Html.ActionLink("Details", "Details", new { id=item.CustomerID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.CustomerID })
        </td>
    </tr>
}
</table>

Create view:

@model WebApplication8.Models.Customer
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) 
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>Customer</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">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

enter image description here

tereško
  • 58,060
  • 25
  • 98
  • 150

1 Answers1

1

You either need a if statement round this block of code

@Html.ActionLink("Create New", "Create")

but i would really recommend you use a viewmodel for views rather than your customer entity.

ViewModels are explained in depth here

Kev Hunter
  • 2,565
  • 4
  • 25
  • 39