MVC :: Updating ViewModel Values On HttpPost?
Sep 3, 2010
I am just getting started with MVC after many years of WebForms development.
I have a very simple page with two textbox fields, one called Input where a user will enter something, and one called Output where I want to return some string after they post the form, much like a postback scenario in WebForms. I have the View connected to a ViewModel for strong typing.
The ViewModel:
[Code]....
The View:
[Code]....
The Controller:
[Code]....
The problem I have is that the output text "Here is some response" never gets displayed, even though I modify the value in the HttpPost method and return the viewmodel to the view. Unless (just tried it) I set the textarea to disabled with:
[Code]....
Can someone explain this behavior? It seems as some "magic" assumes that whatever was in the input fields before the HttpPost should also be there after the post, similar to what ViewState does in WebForms.
View 2 Replies
Similar Messages:
Feb 23, 2011
Simple question. I have an mvc controller that has method:
[code]....
The form is a non-trivial form with a simple textbox.
Question - how on earth do I access the parameter values? I am not posting from a View, the post is coming externally. I'm assuming there is a collection of key/value pairs I have access to. I tried Request.Params.Get("simpleTextBox"); but it returns error "Sorry, an error occurred while processing your request.".
View 3 Replies
Mar 17, 2011
Here is my Controller:
public ActionResult Deposit()
{
return View();
}
[HttpPost]
public ActionResult Deposit(DepositTicket dt)
{
using (var db = new MatchGamingEntities())
{
MembershipUser currentUser = Membership.GetUser();
Guid UserId = (Guid)currentUser.ProviderUserKey;
var AccountId = from a in db.Accounts
where a.UserId == UserId
select a.AccountId;
BankTransaction transaction = new BankTransaction();
transaction.Amount = dt.Amount;
transaction.AccountId = AccountId.SingleOrDefault();
transaction.Created = DateTime.Today;
transaction.TransactionType = "Credit";
Debug.Write("Amount: " + transaction.Amount + " AccountId " + transaction.AccountId);
db.BankTransactions.AddObject(transaction);
db.SaveChanges();
return View();
}
}
Here is my View:
[Code]....
View 1 Replies
Jul 30, 2010
I'm trying to pass the Question parameter from SubmitAnswer to Index.. But it's not working that well...
Code:
[Code]....
View 7 Replies
Jan 8, 2011
Prior to using a ViewModel, I could easily pass the "soon to be edited" object directly to the view without needing to fuss over setting individual properties etc as the View conveniently accepted the Employee type directly..
[HttpGet]
public ActionResult EditEmployee(int? id)
{
EmployeeRepository ER = new EmployeeRepository();
Employee SomeEmployee = ER.GetEmployee(id.Value);
if(SomeEmployee!=null)
return View(SomeEmployee);
But now I'm using a ViewModel with DataAnnotations attributes applied over the top of various properties for validation purposes. Which creates a problem.. After fetching the "soon to be edited" object from the db, setting the ViewModel's values is suddenly a whole lot more complicated. I can't simply pass the retrieved object straight to the view, as the View now expects the VMEmployee type instead. I would like to be able to do something like:
[HttpGet]
public ActionResult EditEmployee(int? id)
{
EmployeeRepository ER = new EmployeeRepository();
Employee SomeEmployee = ER.GetEmployee(id.Value);
if(SomeEmployee!=null)
return View(new VMEmployee(SomeEmployee));
All paths seem to lead to a huge constructor which manually sets the values of each individual property. But I never had to do that before when I wasn't using a ViewModel. Model binding was a blessing! My objects also have complex child objects, which my form is also collecting values for, so this would be a huge/verbose task against DRY principals. I don't even really want to use a ViewModel, but am forced to because I need two different DataAnnotations rule sets for different validation scenarios applied to the same object.
All I want to do is be able to have two different DataAnnotations rule sets for different scenarios. I.e. public-facing www site vs internal-facing admin site. DataAnnotations doesn't seem to be flexible enough to easily cater for this common need. I've tried AutoMapper, but it throws an error saying it can't map my object types, I suspect because Employee was auto-generated by LINQ to SQL. What is the most elegant way to achieve this while sticking to DRY principals?
View 1 Replies
Jun 17, 2010
i am creating inintalizing a viewmodel with the following code in an create action;
[Code]....
the class applications is a LINQ to SQL Class and it has a property called VacancyID.
I want to default this VacancyID to '3'. From within this Action..
View 2 Replies
Nov 19, 2010
I am using Asp.net 3.5 listview control. In item_updating event I am using this code...
dim country as string = Dim country As String = e.NewValues("country_name").ToString.Replace(" ", "")
It's updating record correctly. for example if I update USA to USA 1 1 then it's updating like as it. But I want it to be USA11
View 2 Replies
Sep 4, 2010
I want to use a grid in asp.net MVC application. I did an evaluation and found JQgrid to be optimum from performance point of view(minimum posback). What is your views about this and suggest if you have any other grid in mind which performs better and which is free of cost for commercial usage. Also now a want to post data from edited grid back to server from Jquery base JQgrid .Should i do it using httpget or httppost. If i use HTTPPOST, what should i calculate in controller and what in model As per examples available on net, people are putting calculation logic in controller(See article [URL] I need to know whether it is good practice to put business logic in controller rather than restricting it for routing to view and model.
Controller code as given in mentioned website is having following code
public ActionResult DynamicGridData
(string sidx, string sord, int page, int rows) {
var context = new HaackOverflowDataContext();
int pageIndex = Convert.ToInt32(page) - 1;
int pageSize = rows;
int totalRecords = context.Questions.Count();
int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
var questions = context.Questions
.OrderBy(sidx + " " + sord)
.Skip(pageIndex * pageSize)
.Take(pageSize);
var jsonData = new {
total = totalPages,
page = page,
records = totalRecords,
rows = (
from question in questions
select new {
i = question.Id,
cell = new string[] {
question.Id.ToString(), question.Votes.ToString(), question.Title
}
}).ToArray()
};
return Json(jsonData);
}
design practice and sample to implement grid with HTTPpost feature
View 3 Replies
Nov 22, 2010
I am using a view to create some new categories in the DB but the object the [httpost] create method receives is empty, now this does not always happens. I did double check with other catalogscontrollers and so far the implementation is the same... It only happensds when i try to create a new category the CategoryController
[Code]....
the ViewModel
[Code]....
my create view is strongly typed to ViewModel and has this relevant code create.aspx
[Code]....
and my ascx file is strongly typed to the entity model directly. when the "create" button is pressed and Exception is catched and returns the same view... I have implemented this for other catalogs and works fine except for this one...any clues... by the way i´m using entity model ...
View 3 Replies
Jan 21, 2010
I had a load event in my form where I assign a text box todays date as the text.I have created a button clic event that writes to a database the value which can be updated a required.
The problem i am having is that even if I change the value of the text box it sill seems to remeber the value it was given when the page as loaded.
View 7 Replies
Mar 31, 2010
I am trying to retrive values from the database and fill three text boxes and a drop down value, depending on a user email which the user enters in previous page, Now i have a database which stores these values and i want the user modified values to be stored in the database too.. please help me in retreving the values from the database and updating them back to the database
Name : Textbox1
Location: Textbox2
Gender : Dropdownlist1
[code]...
View 3 Replies
Jan 25, 2011
I'm using Accordion panes and gridviews which are created at run time. For each action item I create an accordion and then populate the tasks that are relevant to that action item in a gridview ( grid view is populated using the second query).
DataRow value dr[1].Tostring ( in the comments below in bold) is not updated with the next row value .
[code]....
View 5 Replies
May 11, 2010
I have a gridview that I'm trying to update the Date something is corrected in the database. When I am putting a new value in, it is updating, but it isn't updating the new value, just the old one that is already there. I cannot figure out why this is happening.
[Code]....
View 8 Replies
Mar 24, 2011
I am using the following code to update the data in my datagrid.
But when I click update the value is updated in the database but it still shows old value in datagrid.
If I refresh the page after that then datagrid shows the updated value.
Code On Update Command:
protected void MySQLDataGrid2_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string newData;
TextBox aTextBox;
aTextBox = (TextBox)(e.Item.Cells[0].Controls[0]);
newData = aTextBox.Text;
decimal comm = Convert.ToDecimal(newData);
string UpdateHiveCommission = "Update tbl_HiveCommission set Commission = '" + Convert.ToDecimal(newData) + "'";
MySqlConnection objMyCon3 = new MySqlConnection(strProvider);
objMyCon3.Open();
MySqlCommand cmd3 = new MySqlCommand(UpdateHiveCommission, objMyCon3);
cmd3.ExecuteNonQuery();
objMyCon3.Close();
MySQLDataGrid2.EditItemIndex = -1;
MySQLDataGrid2.DataBind();
}
View 3 Replies
Apr 13, 2010
I set a breakpoint on my httppost view and found that the value from a checkbox field was 'true,false" even though it was checked..how come it displays both values?
I created the create template using editorformodel(). The field using this checkbox is of bit datatype.
The action code is below;
[Code]....
View 3 Replies
Jan 7, 2011
I am only start learning MVC 2.I have class
[Code].... I wrote HttpPost in controller
[Code]....
my page has next code:
[Code]....
Why InvoiceFinalViewModel view, List<Product> products parameters came with null and zero value in post ?
How correct bind arrays in MVC ?
View 12 Replies
May 6, 2010
I have the following code -
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Always" >
<ContentTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl="~/captcha.aspx" /> [code]....
Problem is the lblname1, lblphone1 and lblemail1 do not update on the page with the new values, if I step into the routine I can see the values and a success asssignments but on the page I get NO values. I removed from code the updatepanel and I get the values on page.I also tried conditional updatemode with no success but again I get NO values.
View 3 Replies
May 25, 2010
How to Retain old values on updating with Stored Procedure
View 6 Replies
Dec 23, 2010
i'm currently having a nightmarish moment with Sitecore. Basically my issues are two-fold:
1) the first time i added a standard value item to a template, and added a layout setup to it, i was thrilled, since all my items from that template were showing the layout, however now, certain items are now showing no layout at all. I tried doing presentation->layout, reset several times, but to no avail.
2) i added a new sublayout to the standard value layout to another placeholder, did publish on the template (republish everything, publish subitems), then publish on the standard values item again, just in case (should not be needed), i click on all items from the template, and they show the updated layout with the new sublayout (should be good, right?) however, i browse, both live and with preview to the pages and i am still seeing the old layout without my new sublayout.
View 2 Replies
Jun 8, 2010
I am using Stored Procedure as my Insert/Update command in a FormView. The sp returns a value which I will need to use. How do I retrieve the value after doing an insert / update?
View 4 Replies
Aug 24, 2013
how to change gridview cell value for a checked row without affecting any changes in database for ex if a row contains quantity of item 6 i want to change it value to 5,4 upto 0 that is on click event of delete quantity button i have identified row with a checkbox in grid view.
View 1 Replies
May 20, 2010
I have just started using MVC2 in VS2010, and I noticed that on the Register page DataType.EmailAddress doesn't actually do any validation, so it does not check whether or not the Email Address entered is valid. Should I write some email validation code in the HttpPost Register method of my AccountController, or is there another way to do this?
View 1 Replies
Mar 5, 2010
I am updating a gridview. Problem is that when I hide certain fields that I do not want the user to update, they lose their values ie. the data is wiped out in the field unless I show them on the grid and make them writeable. Is there a property I need to set for the field to retain the value when the grid is updated or is this in the sql statement?
View 7 Replies
Dec 9, 2010
I'm using ADO.Net code to access and update the data from the database. I'm using GridView for presentation. I wish to preserve old values of all the columns while updaing the records.
I'm using Code behind and not d Data Source controls. I wish to list the old records once record is updated.
View 5 Replies
Apr 6, 2010
I'm trying to execute this statement every minute with SQL Agent, I have it set up as a job:
[Code]....
It's trying to update a column which data type is set to decimal. Just so you know what I really need out of it, here is an example:
[Code]....
The answer of which should add on 0.5 to the original value. If I were to use the increase value of 60 which then becomes 60/60 it will work.I'm dividing by 60 because the increase value is per hour so I make it smaller to get per minute value. Why is it doing this?
View 2 Replies