Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Playing in ViewModel First Time Got issues!!!!!!!!!!

$
0
0

I have not used viewmodels before.  So far I haven't needed too, however now I am trying for the first time.

I have two tables in my database created by EF,  one for client and another for files the client may send us.  For example perhaps a letter of intent, operations contract etc.

IN my view model I have joined the two together:

 public class ClientAndFile
    {
        //[StringLength(12, MinimumLength = 2)]
        public string ShentonAcc { get; set; }

        [Required]
        //[StringLength(120, MinimumLength = 3)]
        [Display(Name = "Business Name")]
        public string Name { get; set; }

        [Required]
        //[StringLength(120, MinimumLength = 3)]
        [Display(Name = "Address")]
        public string Address { get; set; }

        [Required]
        //[StringLength(120, MinimumLength = 3)]
        public string Suburb { get; set; }

        [Required]
        // [StringLength(10, MinimumLength = 3)]
        [Display(Name = "Post Code")]
        public string PostCode { get; set; }


        [Required]
        [Display(Name = "Account Email")]
        //[StringLength(440, MinimumLength = 3)]
        public string AccountEmail { get; set; }

        [Required]
        [Display(Name = "Account Contact")]
        //[StringLength(180, MinimumLength = 3)]
        public string AccountContact { get; set; }


        [Required]
        //[StringLength(20, MinimumLength = 3)]
        [Display(Name = "Office Phone")]
        public string OfficePhone { get; set; }
        public int DocTypeID { get; set; }

        public string Notes { get; set; }

        public string FileName { get; set; }
    }

Working with Google I have managed to modify my view to enable uploading of the file and selection of the document type in my create view.  This is all working fine.

Where I am lost is on the post of the create.  I have:

public class ClientsController : Controller
    {
        private readonly Eva804Context _context;

...other stuff I need... then...
// GET: Clients/Create
        public IActionResult Create()
        {
            var client = new ClientAndFile();

            ViewData["DocTypeID"] = new SelectList(_context.DocType.OrderBy(s => s.DocuType), "DocTypeID", "DocuType");

            return View(client);
        }

        // POST: Clients/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create(
            [Bind("AccountContact,AccountEmail,Address,Name,OfficePhone,PostCode,ShentonAcc,Suburb, DoctypeID, FileName")] ClientAndFile viewModel)
        {
            try
            {

                if (ModelState.IsValid)
                {


                    _context.Add(client);
                    await _context.SaveChangesAsync();
                    return RedirectToAction("Index");
                }
            }

            catch (DbUpdateException /*ex*/)
            {
                //Log the error uncomment ex to write a log
                ModelState.AddModelError("", "Changes not saved.  " +"Try again.  If error recurs Beam Me Up Scotty");
            }
            return View(client);
        }

The get method is working as far as I can tell without posting.

In the post _context.Add(client); is being decorated with a red squiggle.  Looking on Google the examples I have found have only confused me more than help me.

How do I reference the view model within the post method?  Or do I need to map each field back to the original tables?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>