C# ASP.NET MVC - Model gets nulled on POST - c#

I've been struggling with this problem for a while now and I can't find any solution on that solves it for me.
I'm trying to add a string to my model in a view, however when the model gets returned to my HttpPost everything is null except for the string that I'm trying to fill
My model looks like this
namespace WhatsUp.Models {
public class ChatModel
{
public Account user { get; set; }
public Contact contact { get; set; }
public Chat chatA { get; set; }
public Chat chatB { get; set; }
public string newMessage { get; set; }
public ChatModel() { }
public ChatModel(Account user, Contact contact, Chat chatA, Chat chatB)
{
this.user = user;
this.contact = contact;
this.chatA = chatA;
this.chatB = chatB;
}
}
}
My controller looks like this
namespace WhatsUp.Controllers
{
public class ChatsController : Controller
{
IMessageRepository repository = new DbMessageRepository();
IContactRepository contactRepository = new DbContactRepository();
IAccountRepository accountRepository = new DbAccountRepository();
IChatRepository chatRepository = new DbChatRepository();
// GET: Chats
public ActionResult Index()
{
return View();
}
public ActionResult Chat(int contactId)
{
Account user = accountRepository.GetAccount(User.Identity.Name);
Contact contact = contactRepository.GetContact(contactId);
Chat chatA = chatRepository.GetChat(user.id, contact.accountId ?? default(int));
if(chatA == null)
{
chatRepository.CreateChat(user.id, contact.accountId ?? default(int));
}
Chat chatB = chatRepository.GetChat(contact.accountId ?? default(int), user.id);
if(chatB == null)
{
chatRepository.GetChat(user.id, contact.accountId ?? default (int));
}
ChatModel chatModel = new ChatModel(user, contact, chatA, chatB);
return View(chatModel);
}
[HttpPost]
public ActionResult Chat(ChatModel chatModel)
{
repository.SendMessage(new Message(0, chatModel.newMessage, chatModel.chatA.Id));
ModelState.Clear();
return View(chatModel);
}
}
}
And my view
#using WhatsUp.Models
#model ChatModel
#{
ViewBag.Title = "Chat";
}
<h2>Chat with #Model.contact.name</h2>
<div id="chatWindow" style="overflow-y:auto; overflow-x:hidden; height:500px;">
<script>
var element = document.getElementById('chatWindow');
element.scrollTop = element.offsetHeight
</script>
</div>
#using (Html.BeginForm())
{
#Html.TextAreaFor(x => x.newMessage, new { #class = "form-control"})
<input type="submit" value="Send" class="btn btn-primary" />
}

Always use a ViewModel for that kind of things. Follow this steps:
First, create a ViewModel that will contain your message and the chat ids.
Second, ake the ids as a hidden field in your view.
Last thing is, in your POST action, to make sure to get each Chat instance via their id before sending the message.

Related

Implementing MVC design pattern with Repositories and Mapping, C#

I have a problem with implementing the CRUD operations by using repositories, view models, and mapping in my ASP.Net MVC project. The "details"(read the information about one object) and "index"(read the whole list of objects), controllers are working.
I am mapping the Model to ViewModel and then display it in View. But for Create, Update and Delete operations, I should map the ViewModel to the Model. Could you tell me where am I wrong?
Model
public class User
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Unique]
[Required]
[MaxLength(100)]
public string Email { get; set; }
[Required]
public string Password { get; set; }
public string Phone { get; set; }
public bool IsAdmin { get; set; }
}
Base repository
public class BaseRepository<T> : IBaseRepository<T> where T : class
{
private RushHourContext db = null;
private DbSet<T> table = null;
public BaseRepository()
{
this.db = new RushHourContext();
table = db.Set<T>();
}
public BaseRepository(RushHourContext db)
{
this.db = db;
table = db.Set<T>();
}
public IEnumerable<T> SelectAll()
{
return table.ToList();
}
public T SelectByID(object id)
{
return table.Find(id);
}
public void Insert(T obj)
{
table.Add(obj);
}
public void Update(T obj)
{
table.Attach(obj);
db.Entry(obj).State = EntityState.Modified;
}
public void Delete(object id)
{
T existing = table.Find(id);
table.Remove(existing);
}
public void Save()
{
db.SaveChanges();
}
}
Interface for Repository
public interface IBaseRepository<T> where T : class
{
IEnumerable<T> SelectAll();
T SelectByID(object id);
void Insert(T obj);
void Update(T obj);
void Delete(object id);
void Save();
}
Controller
private RushHourContext _db = new RushHourContext();
private IBaseRepository<User> _repository = null;
public UsersController()
{
this._repository = new BaseRepository<User>();
}
public ActionResult Index()
{
if (!LoginUserSession.IsStateAdmin)
{
return RedirectToAction("Login");
}
var users = _repository.SelectAll().ToList();
var userViewModel = Mapper.Map<List<UserViewModel>>(users);
return View(userViewModel);
}
public ActionResult Details(int? id)
{
var users = _repository.SelectByID(id);
var userViewModel = Mapper.Map<UserViewModel>(users);
return View(userViewModel);
}
public ActionResult Create(User user)
{
var users = _repository.Insert(user); // THIS CODE HERE IS WRONG
var userViewModel = Mapper.Map<User>(users);
return View(userViewModel);
}
UserViewModel
public class UserViewModel
{
public int Id { get; set; }
[Required(ErrorMessage = "Please enter User Name.")]
[Display(Name = "User Name")]
public string Name { get; set; }
[MaxLength(100)]
[Display(Name = "Email Address")]
public string Email { get; set; }
[Required]
[Display(Name = "Password")]
public string Password { get; set; }
public string Phone { get; set; }
public bool IsAdmin { get; set; }
}
View
#model RushHour.ViewModels.UserViewModel
#{
ViewBag.Title = "Create";
}
<h2>Create</h2>
<div>
#Html.AntiForgeryToken()
#using (Html.BeginForm("Create", "Users", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
#Html.AntiForgeryToken()
<div>#Html.LabelFor(model => model.Id)</div>
<div>#Html.TextBoxFor(model => model.Id)</div>
<div>#Html.ValidationMessageFor(model => model.Id)</div>
<div>#Html.LabelFor(model => model.Name)</div>
<div>#Html.TextBoxFor(model => model.Name)</div>
<div>#Html.ValidationMessageFor(model => model.Name)</div>
<div>#Html.LabelFor(model => model.Password)</div>
<div>#Html.TextBoxFor(model => model.Password)</div>
<div>#Html.ValidationMessageFor(model => model.Password)</div>
<div>#Html.LabelFor(model => model.Email)</div>
<div>#Html.TextBoxFor(model => model.Email)</div>
<div>#Html.ValidationMessageFor(model => model.Email)</div>
<div>#Html.LabelFor(model => model.Phone)</div>
<div>#Html.TextBoxFor(model => model.Phone)</div>
<div>#Html.ValidationMessageFor(model => model.Phone)</div>
<div>#Html.LabelFor(model => model.IsAdmin)</div>
<div>#Html.TextBoxFor(model => model.IsAdmin)</div>
<div>#Html.ValidationMessageFor(model => model.IsAdmin)</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>
Your model in your Create view is your UserViewModel and that should be passed into the controller, not User.
The pattern you have set up suggests the user will first be on the Create view while entering new user information. So, they will navigate to this view without passing any objects to it on their first trip. If that's the case, you'll want a parameterless constructor for the first visit where you'll just create a new UserViewModel and pass it in. You're then navigating back to this view after user creation which will present a form to them and be very confusing for user experience. You'll probably want to redirect to a confirmation page or the login page with a message saying user created.
Whenever you make changes to the data, EntityFramework won't make those changes until you call SaveChanges on the repository. It's odd that you aren't saving your new user immediately after it being created.
DbSet.Add
DbContext.SaveChanges
For the create function use UserViewModel instead of User. Then map from UserViewModel to User to insert in database. After, if you want to display the saved data, then return UserViewModel object.
Try the following code.
[HttpPost]
public ActionResult Create(UserViewModel userViewModel)
{
var user = new User();
var newUser = Mapper.Map(userViewModel, user);
_repository.Insert(newUser);
_repository.Save();
return View(userViewModel);
}
you can do like this:
[HttpPost]
public ActionResult Create(UserViewModel userViewModel)
{
var user = Mapper.Map<User>(userViewModel);
_reoisitory.Add(user);
}

Dropdownlist from database in MVC5

ViewModel:
public class GroepModel
{
public int id { get; set; }
public String Naam { get; set; }
public String Beschrijving { get; set; }
}
Controller:
public class GroepController : Controller
{
AlinaDatabaseDataContext db = new AlinaDatabaseDataContext();
// GET: Groep
public ActionResult Groepen()
{
List<GroepModel> groepen = Mapper.Map<List<GroepenWerkvorm>, List<GroepModel>>(db.GroepenWerkvorms.ToList());
return View(groepen);
}
}
View
#model alina1617.Models.GroepModel
#{
ViewBag.Title = "Groepen";
}
<h2>Groepen</h2>
<div>
#Html.DropDownListFor(model => model. //This is not working )
</div>
I've looked around and a lot of the things I'm finding solve this using ViewBags, but isn't it suboptimal to use them? So what would be the best approach to get a dropdownlist using a model class with data from a database?
first you need to add an SelectList to your viewModel :
public class MyViewModel {
public SelectList list {get;set;}
public int selectedItem {get;set;}
}
then you need to add your list to the SelectList :
public class GroepController : Controller
{
AlinaDatabaseDataContext db = new AlinaDatabaseDataContext();
// GET: Groep
public ActionResult Groepen()
{
List<GroepModel> groepen = Mapper.Map<List<GroepenWerkvorm>, List<GroepModel>>(db.GroepenWerkvorms.ToList());
var model = new MyViewModel();
model.list = new SelectList(groepen, "id", "Naam");
return View(model);
}
}
and in the view :
#model alina1617.Models.MyViewModel
#{
ViewBag.Title = "Groepen";
}
<h2>Groepen</h2>
<div>
#Html.DropDownListFor(model => model.selectedItem, Model.list )
</div>

Form submission in partial views in MVC

I am developing a simple mvc application . The code is as follows:
Model .cs:
public class CustomModel
{
public IEnumerable<lang> lstlang { get; set; }
public IEnumerable<org> lstOrg { get; set; }
}
public class lang
{
public int langid { get; set; }
public string langName { get; set; }
}
public class org
{
public int orgId { get ;set;}
public string orgName { get; set; }
}
Controller.cs
public Action Index()
{
// Get data from database and fill the model
var model = new CustomModel();
return View(model);
}
public Action Partial()
{
// Get data from database and fill the model
var model = new CustomModel();
return PartialView(model);
}
[HttpPost]
public Action Partial(FormCollection frm, CustomModel model)
{
// Get data from database and fill the model
var model = new CustomModel();
return PartialView(model);
}
Index.cshtml
#model CustomModel
#Html.TextboxFor(x => x.lang.FirstOrDefault().id);
<input type="button" id="btn" />
#Html.RenderPartial("Partial", model)
Partial.cshtml
#model CustomModel
#Html.TextboxFor(x => x.lang.FirstOrDefault().id);
<input type="submit" id="submit" />
The thing is, when I click the submit button in the Partial.cshtml page, and examine the model in httppost method in public Action Partial(FormCollection frm, CustomModel model), the model contains null for both lists lstlang and lstOrg, but the formcollection[0] will give the selected textbox value.
What am I missing, or is this the right way of using partial views?
Don't use FirstOrDefault(). If you want to post something back to the front end with collections, you'll need to use indexing.
Public class CustomModel
{
public ICollection<lang> lstlang { get; set; }
public ICollection<org> lstOrg { get; set; }
}
#HTML.textboxfor(x=>x.lang[0].id);

Issue with editing data and model binding

I started working with asp.net and I have encountered a problem when I try to edit multiple values from a table. I have a bookmark tables which is connected to another tag table, with an 1 : N relationship. My problem is when I want to edit already existing tags associated with an existing url. I can display them on the page but when I try to post the edited data I don't know how to pick it up in the controller. So far I have managed to send them back as a string but I doubt that is the solution since I have to edit all the data again later. I want to replace the existing values in the Tag table with the edited data. Here are my model and controller code snippets.
Bookmark model:
public int id { get; set; }
public string url { get; set; }
public virtual ICollection<Tag> tags { get; set; }
Tag model:
public int id { get; set; }
public string name { get; set; }
public virtual Bookmark bookmark { get; set; }
public string user { get; set; }
Controller:
public ActionResult Edit(int id)
{
var editBookmark = adc.Bookmarks.Single(x => x.id == id);
var query_where2 = from a in adc.Tags
where a.bookmark.id == id
select a;
BookmarkTag bkTag = new BookmarkTag();
bkTag.bookmark = new List<Bookmark>();
bkTag.bookmark.Add(editBookmark);
bkTag.tag = query_where2.ToList();
return View(bkTag.tag);
}
//
// POST: /SavedBookmark/Edit/5
[HttpPost]
public ActionResult Edit(int id, ICollection<FormCollection> tag)
{
try
{
return View();
}
catch
{
return View();
}
Html code:
#using (Html.BeginForm("edit", "SavedBookmark"))
{
#Html.AntiForgeryToken()
if (Model != null) {
var aa= Model.First();
#Html.TextBox("test2", aa.bookmark.url);
List<BookIT2.Models.Tag> allTags = new List<BookIT2.Models.Tag>();
allTags = Model.ToList();
for (int i = 0; i < allTags.Count; i++)
{
if (!allTags[i].name.IsEmpty())
{
#Html.TextBox(allTags[i].name, allTags[i].name);
#Html.Hidden(allTags[i].id.ToString(), allTags[i].id);
#Html.Hidden(allTags[i].user, allTags[i].user)
#Html.Hidden(allTags[i].bookmark.id.ToString(), allTags[i].bookmark.id.ToString())
}
}
#Html.Label("Additional tag")
#Html.TextBox("additionalTag")
<input type="submit" value="edit" />
}
In short: I can't get any values in the http post ICollection, it's always null.
Here is the updated code:
#using (Html.BeginForm("edit", "SavedBookmark"))
{
#Html.AntiForgeryToken()
if (Model != null)
{
for (int i = 0; i < Model.tag.Count; i++)
{
if (!Model.tag[i].name.IsEmpty()) {
#Html.Hidden(Model.tag[i].id.ToString(), Model.tag[i].id);
#Html.Label("name");
#Html.TextBox(Model.tag[i].name, Model.tag[i].name);
#Html.Hidden(Model.tag[i].bookmark.id.ToString(), Model.tag[i].bookmark.id);
#Html.Hidden(Model.tag[i].user, Model.tag[i].user);
}
}
#Html.TextBox(Model.bookmark.id.ToString(), Model.bookmark.url);
<input type="submit" value="edit" />
}
}
Model class:
public class TestBookmark
{
public Bookmark bookmark{get; set;}
public List<Tag> tag {get; set;}
}
[HttpPost]
public ActionResult Edit(TestBookmark edit)
{}
Don't really understand why you're doing it this way. I would like to suggest you totally different approach.
First:
Create a class with all the fields you want in your view.
Second:
Use this class as the MODEL in your View
Third:
In the controller, in the POST function user your class as the only one parameter of that function.

Fill one property of class by selecting one value on dropdownlist of another model

I have a class Client that have some properties in particular one is restriction_type. Also, I create another class Restriction with an ID and a name properties. The name property correspond to the restriction_type.
Then I display the name of all restrictions in my database in the dropdown list:
#Html.ActionLink("Create New", "Create")
#using (Html.BeginForm("AddRestrictions","Restrictions",FormMethod.Get)){
<p> Type de restriction:
#Html.DropDownList("ClientRestr_type", "All")
</p>
<input type="submit"value="Ajouter"/>
}
That is my controller:
public ActionResult AddRestriction(string ClientRestr_type, Restriction restriction)
{
var RestrLst = new List<string>();
var RestrQry = from d in db.Restrictions
orderby d.name
select d.name;
RestrLst.AddRange(RestrQry.Distinct());
ViewBag.ClientRestr_type = new SelectList(RestrLst);
var clients = from c in db.Restrictions select c;
if (string.IsNullOrEmpty(ClientRestr_type))
return View();
else
{
if (ModelState.IsValid)
{
// Here I have maybe to find the way to solve my problem
}
}
So I want to add the name property of Restriction in the restriction_type property of my Model Client.
Model Client:
public class Client
{
[Required]
public int ID
{
get;
set;
}
[Required]
public string compte
{
get;
set;
}
[Required]
public string portefeuille
{
get;
set;
}
[Required]
public String restriction_type
{
get;
set;
}
[Required]
public Boolean etat
{
get;
set;
}
public Boolean decision
{
get;
set;
}
Model Restriction:
public class Restriction
{
public int restrictionID
{
get;
set;
}
public string name
{
get;
set;
}
}
What do you think about my GetRestrictions() method
private SelectList GetRestrictions()
{
var RestrLst = new List<string>();
var RestrQry = from d in db.Restrictions
orderby d.name
select d.name;
RestrLst.AddRange(RestrQry.Distinct());
return new SelectList(RestrLst);
}
But unfortunately I have an error: Impossible to convert System.Web.Mvc.SelectList to MyApp.Models.Client at line:
model.RestrictionList = GetRestrictions();
I don't understand why
Thank you for your help!
A simplified example:
View model
public class ClientVM
{
public Client Client { get; set; }
public SelectList RestrictionList { get; set; }
}
Controller
[HttpGet]
public ActionResult Create()
{
ClientVM model = new ClientVM();
model.Client = new Client();
model.RestrictionList = GetRestrictions(); // your code to return the select list
return View("Edit", model);
}
[HttpGet]
public ActionResult Edit(int ID)
{
ClientVM model = new ClientVM();
model.Client = // call database to get client based on ID
model.RestrictionList = GetRestrictions();
return View(model);
}
[HttpPost]
public ActionResult Edit(ClientVM model)
{
if (!ModelState.IsValid)
{
model.RestrictionList = GetRestrictions();
return View(model);
}
Client client = model.Client;
// Save and redirect
....
}
View
#model YourNamespace.ClientVM
#using (Html.BeginForm() {
#Html.TextBoxFor(m => m.Client.ID)
#Html.TextBoxFor(m => m.Client.compte)
...
#Html.DropDownListFor(m => m.Client.restriction_type, Model.RestrictionList)
<input type="submit" value="Save" />
}

Categories