MVC4 View not binding to Model in Controller on POST - c#

Application View:
#model Models.ApplicationModel
#using (Html.BeginForm())
{
#Html.HiddenFor(m => m.SectionID);
#Html.HiddenFor(m => m.CurrentSectionName);
<div class="section" id="Terms">
#Html.EditorFor(m => m.Term)
</div>
<div class="section" id="User">
#Html.EditorFor(m => m.User)
</div>
<input type="submit" value="Save" />
}
#Scripts.Render("~/bundles/jqueryval")
<script type="text/javascript">
$(function () {
$('form').click(function () {
if ($(this).valid()) {
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
success: function (result) {
debugger;
}
});
}
return false;
});
});
</script>
Application Model:
public class ApplicationModel
{
public int SectionID;
public Term Term;
public User User;
public string CurrentSectionName;
}
Application Controller:
public ActionResult Save(ApplicationModel ApplicationModel, FormCollection fc)
{
return PartialView("Application", ApplicationModel);
}
/EditorTemplates/Term:
#model Data.Term
<fieldset>
<legend>Term</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Type)
#Html.ValidationMessageFor(model => model.Type)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Length)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Length)
#Html.ValidationMessageFor(model => model.Length)
</div>
</fieldset>
/EditorTemplates/User:
#model Data.User
<fieldset>
<legend>User</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.MiddleInitial)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MiddleInitial)
#Html.ValidationMessageFor(model => model.MiddleInitial)
</div>
</fieldset>
When I click the save button, in the Application Controller, only the FormCollection has keys (21 of them). The model is not bound with data.
What am I doing wrong here?

Try this, Add {get; set;} to your model
public class ApplicationModel
{
public int SectionID {get; set;}
public Term Term {get; set;}
public User User {get; set;}
public string CurrentSectionName {get; set;}
}

You may also need to check the access modifier on your Model's properties. I ran into this same situation and while I had setters on my properties, they were protected instead of public. Thank you to Mate for pointing me in the right direction as well.

Related

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.

Check if a specific script is present from an helper/extension method in MVC C#

Is there a way to check in a helper/extension method if a certain script is present in a view?
My idea is to create a custom TextBoxFor() method where the focus is moved to the next input once the present one reach the maxLenght.
why don't you just use javascript for this task?
Example:
http://www.aspdotnet-suresh.com/2013/02/javascript-automatically-move-cursor-to.html
You have JQuery AutoTab Plugin - http://autotab.mathachew.com/
Download the AutoTab Min JS - https://github.com/Mathachew/jquery-autotab/blob/master/js/jquery.autotab.min.js
Lets say I have a model -
public class Details
{
public string Name { get; set; }
public string Email { get; set; }
}
now create a view -
#model YourNameSpave.Details
#{
ViewBag.Title = "GetData";
}
<h2>GetData</h2>
#using (Html.BeginForm()) {
#Html.AntiForgeryToken()
#Html.ValidationSummary(true)
<fieldset>
<legend>Details</legend>
<div class="editor-label">
#Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Name, new { #class = "alphanumeric", maxlength = 10})
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
#Html.TextBoxFor(model => model.Email, new { #class = "alphanumeric", maxlength = 10})
#Html.ValidationMessageFor(model => model.Email)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<script src="~/Scripts/jquery-1.8.2.min.js"></script>
<script src="~/Scripts/jquery.autotab.min.js"></script>
<script>
$(function () {
$('.alphanumeric').autotab();
});
</script>
And now you will get autotab functionality as you wished.

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

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.

How to make my html helpers in razor templates more reusable?

I have a form with multiple addresses. A business address, a billing address, and a shipping address. Naturally I want to use the same code.
My razor template looks something like this:
<div class="std-form-line">
<span class="std-form-no-label"></span>
#Html.TextBoxFor(x => x.BillingAddress.Line2, new { Class = "optional" })
#Html.ValidationMessageFor(x => x.BillingAddress.Line2)
</div>
<div class="std-form-line">
<span class="std-form-no-label"></span>
#Html.TextBoxFor(x => x.BillingAddress.Line3, new { Class = "optional" })
#Html.ValidationMessageFor(x => x.BillingAddress.Line3)
</div>
....
And when rendered looks something like this:
<input type="text" value="" name="BillingAddress.Line1" id="BillingAddress_Line1" data-val-required="The Street Address field is required." data-val="true" class="text-box single-line">
Now say I will have the same code, for Business and shipping but it will look like this:
<div class="std-form-line">
<span class="std-form-no-label"></span>
#Html.TextBoxFor(x => x.BusinessAddress.Line2, new { Class = "optional" })
</div>
<div class="std-form-line">
<span class="std-form-no-label"></span>
#Html.TextBoxFor(x => x.BusinessAddress.Line3, new { Class = "optional" })
</div>
How can I get rid of this code duplication?
The way I do it, is to create strongly typed Display/Editor templates for "Address".
Then call #Html.EditorFor(x=>x.BusinessAddress) or #Html.DisplayFor(x=>x.BusinessAddress)
You can also create different templates for different uses, and then optionally supply the name of the template in the Editor/DisplayFor methods.
BTW, the templates I mention may reside in the ~Views/Shared/EditorTemplates or ~Views/Shared/DisplayTemplates folders, respectively. Or they may reside with in a specific Controllers views folder, under EditorTemplates or DisplayTemplates.
Edit: Here is a link that demonstrates this.
Editor Tempates
Edit 2: Here is an example to try and explain further, per your comment.
Address Model
public partial class Address
{
public int AddressId { get; set; }
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
}
Editor Template: This file is named "Address.cshtml" and located in my "~Views/Shared/EditorTemplates" folder.
#model Address
#Html.HiddenFor(model => model.AddressId)
<div class="editor-label">
#Html.LabelFor(model => model.Street1)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Street1)
#Html.ValidationMessageFor(model => model.Street1)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Street2)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Street2)
#Html.ValidationMessageFor(model => model.Street2)
</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">
#* Populated from Helper *#
#Html.DropDownListFor(m => m.State, new SelectList(statesDictionary, "Key", "Value"), string.Empty)
#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>
Create View: This is my view that will implement the editor template for my model. Note the line #Html.EditorForModel();
#model Address
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<script src="#Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="#Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Address</legend>
#Html.EditorForModel()
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
#Html.ActionLink("Back to List", "Index")
</div>
Now if my model for my view was something else, that linked to an "Address", I would have used #Html.EditorFor(model => model.BusinessAddress)
As for the names, that's completely under your control. By default, they get mapped to you property names, but you can change any by supplying an object containing your html attribues in the EditorFor overloads.
I don't see any duplication to get rid of in the DRY sense. If you have 3 similar fields on a form, you will need three similar elements in the markup.
Why not introduce an AddressType field? All 3 address types you mention share the same fields just their type is different; what will set each appart is the value of AddressType. Make sense?

Categories

Resources