CREATE view does not Post the create method from the controller [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
The create view does not post (or call the create method with the model parameter).
CREATE VIEW
#model Univita.LtcClaims.Models.EpisodeOfBenefitModel
#{
ViewBag.Title = "Create";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Create</h2>
#using (Html.BeginForm(null, "EpisodeOfBenefit",FormMethod.Post)) {
#Html.ValidationSummary(true)
<fieldset>
<legend>EpisodeOfBenefitModel</legend>
<div class="editor-label">
#Html.LabelFor(model => model.episode_of_benefit_id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.episode_of_benefit_id)
#Html.ValidationMessageFor(model => model.episode_of_benefit_id)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.rfb_id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.rfb_id)
#Html.ValidationMessageFor(model => model.rfb_id)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.user_id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.user_id)
#Html.ValidationMessageFor(model => model.user_id)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_status_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_status_cd)
#Html.ValidationMessageFor(model => model.eb_status_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_creation_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_creation_dt)
#Html.ValidationMessageFor(model => model.eb_creation_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_decision_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_decision_dt)
#Html.ValidationMessageFor(model => model.eb_decision_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_start_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_start_dt)
#Html.ValidationMessageFor(model => model.eb_start_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_end_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_end_dt)
#Html.ValidationMessageFor(model => model.eb_end_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_reassessment_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_reassessment_dt)
#Html.ValidationMessageFor(model => model.eb_reassessment_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_revocation_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_revocation_dt)
#Html.ValidationMessageFor(model => model.eb_revocation_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_last_extension_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_last_extension_dt)
#Html.ValidationMessageFor(model => model.eb_last_extension_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_ambulation_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_ambulation_cd)
#Html.ValidationMessageFor(model => model.eb_adl_ambulation_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_bathing_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_bathing_cd)
#Html.ValidationMessageFor(model => model.eb_adl_bathing_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_dressing_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_dressing_cd)
#Html.ValidationMessageFor(model => model.eb_adl_dressing_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_feeding_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_feeding_cd)
#Html.ValidationMessageFor(model => model.eb_adl_feeding_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_incontinence_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_incontinence_cd)
#Html.ValidationMessageFor(model => model.eb_adl_incontinence_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_toileting_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_toileting_cd)
#Html.ValidationMessageFor(model => model.eb_adl_toileting_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_transferring_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_transferring_cd)
#Html.ValidationMessageFor(model => model.eb_adl_transferring_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_abusive_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_abusive_cd)
#Html.ValidationMessageFor(model => model.eb_abusive_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_bizarre_hygiene_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_bizarre_hygiene_cd)
#Html.ValidationMessageFor(model => model.eb_bizarre_hygiene_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_poor_judgement_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_poor_judgement_cd)
#Html.ValidationMessageFor(model => model.eb_poor_judgement_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_wandering_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_wandering_cd)
#Html.ValidationMessageFor(model => model.eb_wandering_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_other_behavior_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_other_behavior_cd)
#Html.ValidationMessageFor(model => model.eb_other_behavior_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_cognitive_imp_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_cognitive_imp_cd)
#Html.ValidationMessageFor(model => model.eb_cognitive_imp_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_complex_unstable_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_complex_unstable_cd)
#Html.ValidationMessageFor(model => model.eb_complex_unstable_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_medicare_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_medicare_cd)
#Html.ValidationMessageFor(model => model.eb_medicare_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_other_ins_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_other_ins_cd)
#Html.ValidationMessageFor(model => model.eb_other_ins_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_living_arr_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_living_arr_cd)
#Html.ValidationMessageFor(model => model.eb_living_arr_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_ra_type_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_ra_type_cd)
#Html.ValidationMessageFor(model => model.eb_ra_type_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_ra_ltr_rcv_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_ra_ltr_rcv_dt)
#Html.ValidationMessageFor(model => model.eb_ra_ltr_rcv_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_status_rsn_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_status_rsn_cd)
#Html.ValidationMessageFor(model => model.eb_status_rsn_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_medication_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_medication_cd)
#Html.ValidationMessageFor(model => model.eb_adl_medication_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_reviewer_user_id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_reviewer_user_id)
#Html.ValidationMessageFor(model => model.eb_reviewer_user_id)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.last_mod_dt)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.last_mod_dt)
#Html.ValidationMessageFor(model => model.last_mod_dt)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.last_mod_by_user_id)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.last_mod_by_user_id)
#Html.ValidationMessageFor(model => model.last_mod_by_user_id)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_wound_care_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_wound_care_cd)
#Html.ValidationMessageFor(model => model.eb_wound_care_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_med_mgmt_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_med_mgmt_cd)
#Html.ValidationMessageFor(model => model.eb_med_mgmt_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_vent_dependent_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_vent_dependent_cd)
#Html.ValidationMessageFor(model => model.eb_vent_dependent_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_adl_dependent_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_adl_dependent_cd)
#Html.ValidationMessageFor(model => model.eb_adl_dependent_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_diabeties_mgmt_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_diabeties_mgmt_cd)
#Html.ValidationMessageFor(model => model.eb_diabeties_mgmt_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_spvn_for_safety_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_spvn_for_safety_cd)
#Html.ValidationMessageFor(model => model.eb_spvn_for_safety_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_not_listed_cd)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_not_listed_cd)
#Html.ValidationMessageFor(model => model.eb_not_listed_cd)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.icd9_code)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.icd9_code)
#Html.ValidationMessageFor(model => model.icd9_code)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.icd9_rcd_type)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.icd9_rcd_type)
#Html.ValidationMessageFor(model => model.icd9_rcd_type)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.eb_living_arr_other_text)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.eb_living_arr_other_text)
#Html.ValidationMessageFor(model => model.eb_living_arr_other_text)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
} <br/>
CONTROLLER
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Univita.LtcClaims.Dal;
using Univita.LtcClaims.Models;
using AutoMapper;
namespace Univita.LtcClaims.Controllers
{
public class EpisodeOfBenefitController : Controller
{
static readonly DbEntities DbContext = DbContextFactory.GetContext();
readonly GenericRepository _repository = new GenericRepository(DbContext);
public IEnumerable<EpisodeOfBenefit> Eobresults = null;
//private DbEntities dbContext = new DbEntities();
//
// GET: /EpisodeOfBenefit/
public ActionResult Index()
{
//Eobresults = dbContext.episode_of_benefit.Where(w => w.rfb_id == 3721130);
Eobresults = _repository.Find<EpisodeOfBenefit>(w => w.RfbId == 3721130);
return View(Eobresults);
}
//
// GET: /EpisodeOfBenefit/Details/5
public ActionResult Details(int id)
{
return View();
}
//
// GET: /EpisodeOfBenefit/Create
[HttpGet]
public ActionResult Create()
{
//Eobresults = _repository.GetQuery<EpisodeOfBenefit>().ToArray();
//Eobresults = _repository.Find<EpisodeOfBenefit>(w => w.RfbId == 3721130);
//Mapper.CreateMap<EpisodeOfBenefit, EpisodeOfBenefitModel>();
//Mapper.Map(EpisodeOfBenefit[], EpisodeOfBenefitModel[])();
return View();
}
//
// POST: /EpisodeOfBenefit/Create
[HttpPost]
//public ActionResult Create(FormCollection collection)
public ActionResult Create(EpisodeOfBenefitModel episodeOfBenefit)
{
try
{
// TODO: Add insert logic here
_repository.Add(episodeOfBenefit);
_repository.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View(episodeOfBenefit);
}
}
MODEL
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Univita.LtcClaims.Models
{
public class EpisodeOfBenefitModel
{
//this.eob_service_detail_paid_day = new HashSet<eob_service_detail_paid_day>();
//this.plan_of_care = new HashSet<plan_of_care>();
[Key]
public int episode_of_benefit_id { get; set; }
public int rfb_id { get; set; }
public string user_id { get; set; }
public short eb_status_cd { get; set; }
public DateTime eb_creation_dt { get; set; }
public DateTime eb_decision_dt { get; set; }
public DateTime eb_start_dt { get; set; }
public DateTime eb_end_dt { get; set; }
public DateTime eb_reassessment_dt { get; set; }
public DateTime eb_revocation_dt { get; set; }
public DateTime eb_last_extension_dt { get; set; }
public short eb_adl_ambulation_cd { get; set; }
public short eb_adl_bathing_cd { get; set; }
public short eb_adl_dressing_cd { get; set; }
public short eb_adl_feeding_cd { get; set; }
public short eb_adl_incontinence_cd { get; set; }
public short eb_adl_toileting_cd { get; set; }
public short eb_adl_transferring_cd { get; set; }
public short eb_abusive_cd { get; set; }
public short eb_bizarre_hygiene_cd { get; set; }
public short eb_poor_judgement_cd { get; set; }
public short eb_wandering_cd { get; set; }
public short eb_other_behavior_cd { get; set; }
public short eb_cognitive_imp_cd { get; set; }
public short eb_complex_unstable_cd { get; set; }
public short eb_medicare_cd { get; set; }
public short eb_other_ins_cd { get; set; }
public short eb_living_arr_cd { get; set; }
public short eb_ra_type_cd { get; set; }
public DateTime eb_ra_ltr_rcv_dt { get; set; }
public short eb_status_rsn_cd { get; set; }
public short eb_adl_medication_cd { get; set; }
public string eb_reviewer_user_id { get; set; }
public DateTime last_mod_dt { get; set; }
public string last_mod_by_user_id { get; set; }
public short eb_wound_care_cd { get; set; }
public short eb_med_mgmt_cd { get; set; }
public short eb_vent_dependent_cd { get; set; }
public short eb_adl_dependent_cd { get; set; }
public short eb_diabeties_mgmt_cd { get; set; }
public short eb_spvn_for_safety_cd { get; set; }
public short eb_not_listed_cd { get; set; }
public string icd9_code { get; set; }
public string icd9_rcd_type { get; set; }
public string eb_living_arr_other_text { get; set; }
//public virtual eb_living_arr eb_living_arr { get; set; }
//public virtual ICollection<eob_service_detail_paid_day> eob_service_detail_paid_day { get; set; }
//public virtual request_for_benefit request_for_benefit { get; set; }
//public virtual ICollection<plan_of_care> plan_of_care { get; set; }
}
}

You should change
#using (Html.BeginForm(null, "EpisodeOfBenefit",FormMethod.Post))
To
#using (Html.BeginForm("Create", "EpisodeOfBenefit",FormMethod.Post))
in your code, you try to post to Action with name null.

Related

field is not created or edited

Field is not created or edited in views Create and Edit (C# MVC4 CodeFirst).
The rest of the field with the data types string or integer created and edited properly and correctly.
In Model Requirement:
...
public int RequirementId { get; set; }
//[StringLength(500, MinimumLength = 500)]
public string Definition { get; set; }
public string Rationale { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
public DateTime CreatedOn { get; set; }
public virtual Users CreatedBy { get; set; }
public virtual Users Responsible { get; set; }
public virtual ICollection<Users> InterestedPersons { get; set; }
public string CurentVersion { get; set; }
public StateEnum State { get; set; }
public PriorityEnum Priority { get; set; }
public StabilityEnum Stability { get; set; }
public TypeEnum Type { get; set; }
public virtual BusinessRule Source { get; set; }
public virtual ICollection<TestCase> TestCase { get; set; }
public string UserPart { get; set; }
...
Controller RequirementController (in method Create):
public ActionResult Create()
{
RequirementViewModel reqVM = new RequirementViewModel();
AutoMapper.Mapper.CreateMap<RequirementViewModel, Requirement>();
reqVM.UserList = new SelectList(db.Users, "UsersId", "Surname");
reqVM.BusinessRuleList = new SelectList(db.BusinessRule, "BusinessRuleId", "Definition");
reqVM.TestCaseList = new SelectList(db.TestCase, "TestCaseId", "Title");
Requirement requirement = AutoMapper.Mapper.Map<RequirementViewModel, Requirement>(reqVM);
return View();
}
//
// POST: /Requirement/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Requirement requirement)
{
try
{
AutoMapper.Mapper.CreateMap<RequirementViewModel, Requirement>();
reqVM.UserList = new SelectList(db.Users, "UsersId", "Surname");
reqVM.BusinessRuleList = new SelectList(db.BusinessRule, "BusinessRuleId", "Definition");
reqVM.TestCaseList = new SelectList(db.TestCase, "TestCaseId", "Title");
Requirement requirement = AutoMapper.Mapper.Map<RequirementViewModel, Requirement>(reqVM);
if (ModelState.IsValid)
{
db.Requirement.Add(requirement);
db.SaveChanges();
return RedirectToAction("Index"); //, new { id = requirement.InterestedPersons }
}
}
catch (DataException /* dex */)
{
//Log the error (uncomment dex variable name after DataException and add a line here to write a log.
ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
}
return View();
}
View Create:
...
#model OpenSoft.AgileAnalytics.EF.Models.Requirement
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Requirement</legend>
#*#Html.EditorForModel()*#
<div class="editor-label">
#Html.LabelFor(model => model.Definition)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Definition)
#Html.ValidationMessageFor(model => model.Definition)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Rationale)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Rationale)
#Html.ValidationMessageFor(model => model.Rationale)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CreatedOn)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CreatedOn)
#Html.ValidationMessageFor(model => model.CreatedOn)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CreatedBy)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.CreatedBy, Model.UserList, "-Select-") //error
#Html.ValidationMessageFor(model => model.CreatedBy)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Responsible)
</div>
<div class="editor-field">
#Html.DropDownListFor(model=>model.Responsible, Model.UserList, "-Select-") //error
#Html.ValidationMessageFor(model => model.Responsible)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.InterestedPersons)
</div>
<div class="editor-field">
#Html.ListBoxFor(model=>model.InterestedPersons, Model.UserList) //error
#Html.ValidationMessageFor(model => model.InterestedPersons)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.CurentVersion)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.CurentVersion)
#Html.ValidationMessageFor(model => model.CurentVersion)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.UserPart)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.UserPart)
#Html.ValidationMessageFor(model => model.UserPart)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
#*#Html.EnumDropDownList(model => model.StateEnum)*#
#Html.DropDownListFor(m => m.State, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.StateEnum))))
#Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Priority)
</div>
<div class="editor-field">
#Html.DropDownListFor(m => m.Priority, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.PriorityEnum))))
#Html.ValidationMessageFor(model => model.Priority)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Stability)
</div>
<div class="editor-field">
#Html.DropDownListFor(m => m.Stability, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.StabilityEnum))))
#Html.ValidationMessageFor(model => model.Stability)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
#Html.DropDownListFor(m => m.Type, new SelectList(Enum.GetValues(typeof(OpenSoft.AgileAnalytics.EF.Models.TypeEnum))))
#Html.ValidationMessageFor(model => model.Type)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Source)
</div>
<div class="editor-field">
#Html.DropDownList("BusinessRuleId", "-Select-")
#Html.ValidationMessageFor(model => model.Source)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.TestCase)
</div>
<div class="editor-field">
#Html.DropDownList("TestCaseId", "-Select-")
#Html.ValidationMessageFor(model => model.TestCase)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
...
In the drop-down lists the elements of the right.
Help please with the work with these fields model:)
Your properties CreatedBy, Responsible and InterestedPersons are complex objects. A <select> only posts back a single value (or array of values in the case of multiple). In addition the name of your dropdowns are all UsersId but your model does not contain a property named UsersId so nothing is bound.
Create a view model to represent the data you want to edit.
public class RequirementVM
{
public int RequirementId { get; set; }
public string Definition { get; set; }
.... // other properties of Requirement that you want to edit
[Required]
[Display(Name="Created by")]
public int? CreatedBy { get; set; }
[Required]
public int? Responsible { get; set; }
public int[] InterestedPersons { get; set; }
public SelectList UserList { get; set; }
}
Controller
public ActionResult Create()
{
RequirementVM model = new MyViewModel();
model.UserList = new SelectList(db.Users, "UsersId", "Surname");
return View(model);
}
[HttpPost]
public ActionResult Create(RequirementVM model)
{
...
}
View
#model RequirementVM
....
#Html.LabelFor(model => model.Definition)
#Html.EditorFor(model => model.Definition)
#Html.ValidationMessageFor(model => model.Definition)
... other properties of RequirementVM to edit
#Html.LabelFor(m => m.CreatedBy)
#Html.DropDownListFor(m => m.CreatedBy, Model.UserList, "-Select-")
#Html.ValidationMessageFor(m => m.CreatedBy)
#Html.LabelFor(m => m.Responsible)
#Html.DropDownListFor(m => m.Responsible, Model.UserList, "-Select-")
#Html.ValidationMessageFor(m => m.Responsible)
#Html.LabelFor(m => m.InterestedPersons)
#Html.ListBoxFor(m => m.InterestedPersons, Model.UserList)
#Html.ValidationMessageFor(m => m.InterestedPersons)

CreateView and Controller for an Multiple Model with list .net mvc

i've got a entity made in this way,
public class UserTest : IdentityEntity
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Email { get; set; }
public virtual string Phone { get; set; }
public virtual string Fax { get; set; }
public virtual DateTime BirthDate { get; set; }
public virtual IList<UserAddress> Address { get; protected set; }
}
public class UserAddress : IdentityEntity
{
public virtual string Address { get; set; }
public virtual string City { get; set; }
public virtual string Province { get; set; }
}
Every time i associated one Create View with one Model, but this time (and i wonder a lot of time's next) i will need to made a Create View with more than one Entity like this case.
Using Scaffolding Tool, VS doesn't prepare view for this List.
How to proceed?
We have something similar in our application where we allow admin to create new user. Models are similar to what you have. To render a list of addresses in Create User view, we have used Display and Editor templates.
These templates are similar to partial view and stored in Shared folder under "DisplayTemplates" or "EditorTemplates" folder.
Shared\DisplayTemplates\AddressViewModel.cshtml
#model Eda.RDBI.Web.Models.AddressViewModel
<h3>#Model.BillingType.ToString() Address</h3>
#Html.HiddenFor(model => model.Id)
#Html.HiddenFor(model => model.OrganizationId)
#Html.HiddenFor(model => model.BillingType)
<div class="editor-label">
#Html.LabelFor(model => model.ContactFirstName)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.ContactFirstName)
#Html.ValidationMessageFor(model => model.ContactFirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ContactLastName)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.ContactLastName)
#Html.ValidationMessageFor(model => model.ContactLastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Street1)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Street1)
#Html.ValidationMessageFor(model => model.Street1)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Street2)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Street2)
#Html.ValidationMessageFor(model => model.Street2)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.City)
#Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.State)
#Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Country)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Country)
#Html.ValidationMessageFor(model => model.Country)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Zip)
#Html.ValidationMessageFor(model => model.Zip)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PhoneNumber)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.PhoneNumber)
#Html.ValidationMessageFor(model => model.PhoneNumber)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Extension)
</div>
<div class="editor-field">
#Html.DisplayFor(model => model.Extension)
#Html.ValidationMessageFor(model => model.Extension)
</div>
Shared\EditorTemplates\AddressViewModel.cshtml
It has similar elements but uses EditorFor. I am not pasting it here as it is a big code.
Once you have templates in right folder, in your main view e.g. User\Create.cshtml, along with other html elements like userName, age, etc. have below line:
<div class="row">
#Html.EditorFor(m => m.Addresses)
</div>
You can find example of how to use Display or Editor template in mvc online for example here. Also see this link for more help on templates.
.
Hope this helps.

Adding a new object to a SQL Server database using LINQ C#/MVC does simply returns me to the same page without adding records to the database

My Controller
[HttpGet]
public ActionResult Create()
{
return View();
}
[HttpPost]
public ActionResult Create(Customers Customer)
{
if (ModelState.IsValid)
{
_db.Customers.Add(Customer);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View(Customer);
}
My Model:
public class Customers
{
public int ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set;}
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public string Email { get; set; }
// public ICollection<CustomerData> Customers { get; set; }
}
From my View:
<p>
#Html.ActionLink("Create New", "Create")
Create.cshtml:
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Add New Customer</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Address)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Address)
#Html.ValidationMessageFor(model => model.Address)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.City)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.City)
#Html.ValidationMessageFor(model => model.City)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.State)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.State)
#Html.ValidationMessageFor(model => model.State)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Zip)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Zip)
#Html.ValidationMessageFor(model => model.Zip)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Phone)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Phone)
#Html.ValidationMessageFor(model => model.Phone)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Email)
#Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
}
When I run this code, all of the fields end up getting rendered from the subsequent Create.cshtml. Filling in all of the forms and clicking the submit button sends me back to "Index", but does not add any of my fields into the database. It seems like the Create action is not being hit. Anyone have any advice for a beginner?
First off, thanks for everyone responding so quickly. Turns out, I had my db set to deploy on debug somehow. When I unchecked deploy on my project debug properties, everything worked.

Drop down and Radio button List for string field in MVC 4

I have String field for Gender and Hospital Category in My MVC 4 (code First) Model Class. I want to have a radio button list for Gender showing Male and Female and Save "Male" or "Female" according to selection in my view instead of a text box which is added when generating the view. And for Hospital I want to show list of Hospital Categories in a d drop down, example, Government, Private etc. and the one selected will be saved to the Hospital Categories. I know if i use to have another class and make a relation by ID fields. but since i already have data in those fields and the application is running, I was looking for any shortcut. please help me.
Model class:
public class MedicalDetail
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
public string CaseNo
{
get
{
if (this.RimsMain != null)
return this.RimsMain.CaseNoNew;
else
return "N/A";
}
}
[Required]
[ForeignKey("RimsMain")]
public int RimsMainId { get; set; }
public virtual RimsMain RimsMain { get; set; }
[Required]
public string Name { get; set; }
[DisplayFormat(DataFormatString = "{0:d}")]
[Column(TypeName = "DateTime2")]
public DateTime Dob { get; set; }
public string Nationality { get; set; }
[DisplayFormat(DataFormatString = "{0:d}")]
[Column(TypeName = "DateTime2")]
[DisplayName("Examin Date")]
public DateTime ExaminDate { get; set; }
public string Gender { get; set; }
[DisplayName("Referred For Hospital")]
public string ReferredForHospital { get; set; }
[DisplayName("Madicine Provided")]
public string MadicineProvided { get; set; }
public string Demand { get; set; }
public string Reimbursement { get; set; }
[DisplayName("Milk Assistance")]
public string MilkAssistance { get; set; }
[DisplayName("Chronic Case")]
public string ChronicCase { get; set; }
[DisplayName("Special Case")]
public string SpecialCase { get; set; }
[DisplayName("Gyne Case")]
public string GyneCase { get; set; }
[DisplayName("Referred By UNHCR")]
public bool ReferralByUNHCR { get; set; }
[MaxLength]
public string LabInvestigation { get; set; }
[MaxLength]
public string Diagnosis { get; set; }
public string HospitalCategory { get; set; }
[MaxLength]
public string Recommendation { get; set; }
[DisplayName("Last Updated by")]
public int? UserId { get; set; }
[DisplayFormat(DataFormatString = "{0:d}")]
[Column(TypeName = "DateTime2")]
[DisplayName("Last Updated")]
public DateTime? LastUpdated { get; set; }
}
view:
#model Rims.Models.MedicalDetail
#{
ViewBag.Title = "Create";
}
#using (Html.BeginForm())
{
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Medical Detail</legend>
<h4 style="color:#ViewBag.Color;">#ViewBag.Message</h4>
<div>
<div class="col2">
<div class="editor-label">
<label for="RimsMainId">Case No</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.RimsMain.CaseNoNew)
#Html.ValidationMessageFor(model => model.RimsMain.CaseNoNew)
</div>
<div class="editor-label">
<label for="Name">Name</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
<label for="Dob">DOB</label>
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Dob, new { #class = "form-control datepicker" })
#Html.ValidationMessageFor(model => model.Dob)
</div>
<div class="editor-label">
<label for="Nationality">Nationality</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Nationality)
#Html.ValidationMessageFor(model => model.Nationality)
</div>
<div class="editor-label">
<label for="ExaminDate">Examin Date</label>
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.ExaminDate, new { #class = "form-control datepicker" })
#Html.ValidationMessageFor(model => model.ExaminDate)
</div>
<div class="editor-label">
<label for="Gender">Gender</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Gender)
#Html.ValidationMessageFor(model => model.Gender)
</div>
</div>
<div class="col2">
<div>
<div class="editor-label">
<label for="ReferredFor">Referred by UNHCR</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ReferralByUNHCR)
#Html.ValidationMessageFor(model => model.ReferralByUNHCR)
</div>
<div class="clear"></div>
</div>
<div class="editor-label">
<label for="MadicineProvided">Madicine Provided</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MadicineProvided)
#Html.ValidationMessageFor(model => model.MadicineProvided)
</div>
<div class="editor-label">
<label for="MilkAssistance">Milk Assistance</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MilkAssistance)
#Html.ValidationMessageFor(model => model.MilkAssistance)
</div>
<div class="editor-label">
<label for="MilkAssistance">Chronic Case</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ChronicCase)
#Html.ValidationMessageFor(model => model.ChronicCase)
</div>
<div class="editor-label">
<label for="MilkAssistance">Special Case</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.SpecialCase)
#Html.ValidationMessageFor(model => model.SpecialCase)
</div>
<div class="editor-label">
<label for="MilkAssistance">Gyne Case</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.GyneCase)
#Html.ValidationMessageFor(model => model.GyneCase)
</div>
</div>
<div class="clear"></div>
</div>
<div class="editor-label">
<label for="Demand">Demand</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.Demand, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.Demand)
</div>
<div class="editor-label">
<label for="ReimbursemenT">Reimbursement</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.Reimbursement, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.Reimbursement)
</div>
<div class="editor-label">
<label for="Referred">Referred for Hospital</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.ReferredForHospital, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.ReferredForHospital)
</div>
<div class="editor-label">
<label for="Remarks">Lab Investigation</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.LabInvestigation, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.LabInvestigation)
</div>
<div class="editor-label">
<label for="Remarks">Diagnosis</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.Diagnosis, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.Diagnosis)
</div>
<div class="editor-label">
<label for="Remarks">Hospital Category</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.HospitalCategory, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.HospitalCategory)
</div>
<div class="editor-label">
<label for="Remarks">Recommendation</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.Recommendation, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.Recommendation)
</div>
<p id="otherButtons1">
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div id="otherButtons">
#Html.ActionLink("Back to List", "Index")
</div>
In the Above view I want to replace the the following Field by Two Radio Buttons that is Male and Female
<div class="editor-label">
<label for="Gender">Gender</label>
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Gender)
#Html.ValidationMessageFor(model => model.Gender)
</div>
And The Following field by a drop down which will have some values. for example "Gov", "Private" etc.
<div class="editor-label">
<label for="Remarks">Hospital Category</label>
</div>
<div class="editor-field">
#Html.TextAreaFor(model => model.HospitalCategory, 5, 70, new { #class = "txtArea" })
#Html.ValidationMessageFor(model => model.HospitalCategory)
</div>
also I want to know how can i save the value in Create Action:
[HttpPost]
[Authorize(Roles = "Admin,Medical")]
[ValidateAntiForgeryToken]
public ActionResult Create(MedicalDetail medicaldetail)
{
if (ModelState.IsValid)
{
var rimsMain = db.RimsMains.Where(x => x.CaseNoNew == medicaldetail.RimsMain.CaseNoNew).FirstOrDefault();
if (rimsMain == null)
{
ViewBag.Message = "Invalid Case Number!";
ViewBag.Color = "Red";
return View(medicaldetail);
}
else
{
medicaldetail.RimsMainId = rimsMain.Id;
//medicaldetail.RimsMain = rimsMain;
medicaldetail.LastUpdated = DateTime.Now;
medicaldetail.UserId = WebSecurity.CurrentUserId;
db.MedicalDetails.Add(medicaldetail);
db.SaveChanges();
return RedirectToAction("Create", new { message = "Record Added Successfully!", color = "Green" });
}
}
return View(medicaldetail);
}
For Gender selection you need to use Radio buttons with same name ::
<input type="radio" name="Gender" value="Male"/>Male
<br/>
<input type="radio" name="Gender" value="Female"/>Female
and for Company list you should use viewbag as::
#Html.DropDownList("Company", "Select Company")
and on Server side the Viewbag will be formed as ::
ViewBag.Company = new SelectList(context.Companies, "ID", "Name");

Validation errors when trying to save image source in database and image in file system

I am trying to save an image path in my database and the actual image in the file system. File is copied perfectly but it's source does not get inserted into the database, it's always null in ModelState.IsValid function
Here is my movie class
public class Movie
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int MovieId { get; set; }
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Genre is required")]
public string Genre { get; set; }
[Required(ErrorMessage = "Rating is required")]
public int Rating { get; set; }
[Required(ErrorMessage = "Description is required")]
public string Description { get; set; }
[Required(ErrorMessage = "Date is required")]
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
[Required(ErrorMessage = "Image is required")]
public string ImageSource { get; set; }
}
Here is my create view
#using (Html.BeginForm("Create","Movies",FormMethod.Post,new { enctype = "multipart/form-data" })) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Movie</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Genre)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Genre)
#Html.ValidationMessageFor(model => model.Genre)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Rating)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Rating)
#Html.ValidationMessageFor(model => model.Rating)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Description)
#Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ReleaseDate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ReleaseDate)
#Html.ValidationMessageFor(model => model.ReleaseDate)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ImageSource)
</div>
<div class="editor-field">
<input type="file" name="uploadImages" multiple="multiple" class="input-files" />
#Html.ValidationMessageFor(model => model.ImageSource)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
Here is my action method
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(Movie movie)
{
foreach (string file in Request.Files)
{
var postedFiles = Request.Files[file];
postedFiles.SaveAs(Server.MapPath("~/Content/Images/") + Path.GetFileName(postedFiles.FileName));
}
db.Movies.Add(movie);
db.SaveChanges();
return RedirectToAction("Index");
}
ImageSource is always null and causing a validation error for one or more entities.
You are not assigning ImageSource anywhere, either remove ImageSource from the model and use the filename (or fullpath) to write that to the database.
Or use javascript to assign the filename to ImageSource.
Edit: Or just remove the Required attribute from ImageSource.

Categories

Resources