MVC- Retrieve the original value of an EditorFor - c#

On the edit page of my website I want users to be able to edit qty's and then have it change the model. This is why I am using 'editorFor'. The problem is that I still want to return the value of the model before it was edited to the controller post method.
For example, if a user edited a qty from 7 to 10, I would want the model to change to 10 but I would also want the view to return 7 to the controller. How can I do this?
Here is my editorFor code
<div class="form-group">
#Html.LabelFor(model => model.item_qty, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.item_qty, new { htmlAttributes = new { #class = "form-control" } })
#Html.ValidationMessageFor(model => model.item_qty, "", new { #class = "text-danger" })
</div>
</div>
Here is my edit post method
[RestrictAccess(restriction = AccessRestrictions.ModifyWorkOrder)]
[HttpPost]
[ValidateAntiForgeryToken]
[Audit]
public ActionResult Edit([Bind(Include = "ID,JobOrderID,StartDate,CompleteDate,jobSection,ItemID,item_qty,actual_item_qty,ComponentID,comp_qty,actual_comp_qty,PartID,part_qty,actual_part_qty,Notes,subDetail")] JODetails jODetails)
{
if (ModelState.IsValid)
{
JobOrder jo = db.JobOrders.Find(jODetails.JobOrderID);
db.Entry(jODetails).State = EntityState.Modified;
JODetails currentData = db.JODetails.Find(jODetails.ID);
Component comp = db.Components.Find(jODetails.ComponentID);
Item i = db.Items.Find(jODetails.ItemID);
int newItemCount = jODetails.item_qty != null ? (int)jODetails.item_qty : 0;
int oldItemCount = 0;
int itemDiff = newItemCount - oldItemCount;
}

If I understand well enough, you don't need to return the value from the view. When the model is passed to the controller the value in the database has not been changed, you can read it again. Something like this:
// First. Grab the previous quantity...
// This approach won't work because of EF is already TRACKING the entity
// JODetails currentData = db.JODetails.Find(jODetails.ID);
// So, you have to access data like this...
var currentData = db.JODetails.AsNoTracking().FirstOrDefault(j => j.ID == jODetails.ID);
int previousQuantity = currentData?.item_qty;
//... do whatever with previousQuantity prop & new JODetails model. Don't forget to check if currentData is null
// Just NOW. Mutate the object via EF...
db.Entry(jODetails).State = EntityState.Modified;
// THEN. Commit changes...
db.SaveChanges();
It's not until you SaveChanges() that the record in the database it's really changed. Read some docs about how EF tracks, modifies & save objects.
This could be very helpful to understand this...
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/examining-the-edit-methods-and-edit-view
Of course if you NEED to retrieve the Quantity data from the view (can't see why) there are some alternatives such as a ViewModel but I think this would do the trick. Yes, with this approach there's one more trip to the database, but it's minimal.

Related

.NET Display different database variable but save as ID?

I don't know how to sum up what I'm trying to do in the title properly! Basically I have created a controller with EF6 and one foreign key of the Client table is MJTopicsID which links to the MJTopics table, a table of 26 topics. In the add and edit view I want the MJTopicsID to be a drop down menu displaying all the topics available however when you select it and click to add or edit an entry it adds it to the Client table as the MJTopicsID foreign key number? How do I go about this so I can apply it to all of my views.
This is the dropdown menu I created in the edit view however it just shows numbers 1-26 and if I change it to the topics variable it doesn't know it as obviously its not in the open model, thanks!
<div class="form-group">
#Html.LabelFor(model => model.MJTopicsID, "MJTopicsID", htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("MJTopicsID", null, htmlAttributes: new { #class = "form-control" })
#Html.ValidationMessageFor(model => model.MJTopicsID, "", new { #class = "text-danger" })
</div>
</div>
If I understood correctly then you want to display some readable value and save some db specific value like foreign key Id in this case.
In the view for helper method "DropDownList" you can give second parameter of type IEnumerable type as a list of your options like this:
View:
#Html.DropDownList("MJTopicsID",
(List<SelectListItem>)#ViewBag.MJTopics,
htmlAttributes: new { #class = "form-control" })
Action:
public ActionResult Index()
{
ViewBag.MJTopics = new List<SelectListItem>() {
new SelectListItem(){Text = "Topic1", Value ="1" },
new SelectListItem(){Text = "Topic2", Value ="2" }
};
return View();
}
SelectListItem has two properties which you can map with your models.
Also you can use your model with "Text" as value to display and "value" as value to use.
Let "topics" is your list model for topics
then you can write it as
topics.Select(
x=> new SelectListItem()
{
Text = x.Id,
value =x.Desc
})
This is the one way of doing it.
I hope it will solve your problem.
I wanted to add an answer if anyone is new to EF6 and is trying to do the same thing.
In the controller for the actionresult you are trying to do this for, so in my case create it will create a viewbag variable this was mine:
ViewBag.MJTopicsID = new SelectList(db.MJTopicss, "ID", "ID");
I changed it to:
ViewBag.MJTopicsID = new SelectList(db.MJTopicss, "ID", "topicalTF");
The topicalTF is the variable for the topics available and this showed all the topics but would save it as the ID, hope this helps!

DropDownListFor How to set a default value from a database

So, I got a DropdownListfor that looks like that in my view:
#Html.DropDownListFor(m => m.ProductCategory, new SelectList(Model.ProductCategories.OrderBy(m => m.PCNumber), "", "Name"), "")
That works like it should. In my next view the user should be able to edit his order. So what I want to do is, if he opens the form all of his data from before should be displayed, for textboxes I got it work with the following code:
#Html.TextBoxFor(m => m.NameOfProduct, new { #Value = #Model.NameofProduct })
So now my problem is how can I do the same thing that I did with my textboxes (giving them default values from my model) for a DropDownListFor when the value is stored in my model(database)? It should like that if he selected Category 3 before and now wants to edit his order from before, the dropdownlist should show category 3 right away.
Thank you for any help!
Try this code may be it work in your situation.
#Html.DropDownListFor(m=> m.PCNumber, new SelectList(m.ProductCategories, "PCNumber", "ProductCategory"), "-- Select Category --", new { #class = "form-control" })
here you will get default edit order in your dropdown
[HttpGet]
public ActionResult EditOrder(int? id)
{
_obj_order_detail = db.order_detail.Find(id);
if (_obj_order_detail != null)
{
_obj_order_detail.ProductCategories = db.category_detail.ToList(); //get category List
return View(_obj_order_detail);
}
else
{
return view();
}
}
this will return view with order which you want to edit and ProductCategories and dropdown bu default contain ProductCategory which you want to edit

How to save my dropdown list selected Item into my database?

I need to save a selected Item in my Database during User Registration. but it seems as if my selected Item is not recognised. here is the error that it's givin me
"There is no ViewData item of type 'IEnumerable' that
has the key 'Faculties"
Am still unskilled in MVC/C# Programming please help here is my code below; thanks in advance!
My DataModel
public string Faculty { get; set; }
My Controller
public ActionResult Register()
{
DataClasses1DataContext db = new DataClasses1DataContext();
ViewBag.Faculties = new SelectList(db.Faculties, "Id", "Name");
return View();
}
My View
<div class="form-group">
#Html.LabelFor(model => model.Faculty, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.DropDownList("Faculties","Select Faculty")
#Html.ValidationMessageFor(model => model.Faculty, "", new { #class = "text-danger" })
</div>
</div>
The way you are displaying items in the dropdown is not correct. You can use below code to display the items fetched from your db:
#Html.DropDownList("Faculties", ViewBag.Faculties as IEnumerable<SelectListItem>,
"Select Faculty");
Please note that your ViewBag.Faculties should be casted to Enumerable<SelectListItem>.
To get the selected value of dropdown in controller you can use below method:
var value = Request["Faculties"];
Once you got the value, you can save it in database.
Update:
A good approach will be to bind your View to a model which I think you have already done since I can see you are using model.Faculty. So the dropdown should look something like below in View:
#Html.DropDownList(model => model.Faculty,ViewBag.Faculties as IEnumerable<SelectListItem>,
"Select Faculty");
And your controller where data is posted should be something like below:
[HttpPost]
public ActionResult Register(YourModel model)
{
var selectedFaculty = model.Faculty; //Selected Value
//Save it in database
}
Try changing this line:
ViewBag.Faculties = new SelectList(db.Faculties, "Id", "Name");
to the following
ViewData["Faculties"] = new SelectList(db.Faculties, "Id", "Name");
ViewBag and ViewData are two separate constructs, and cannot be used interchangeably.
It's just the names of your property and your ViewBag are different. change your ViewBag name to match the property name.
ViewBag.Faculty = new SelectList(db.Faculties, "Id", "Name");
Your HTML would be:
#Html.DropDownList("Faculty ","Select Faculty")
Alternatively and (preferably) use a model binding instead of ViewBag
Model
public string Faculty { get; set; }
public IList<SelectListItem> Faculties {get;set;}
Controller
Model.Faculties = new SelectList(db.Faculties, "Id", "Name");
return View(Model);
HTML (View)
#Html.DropDownListFor(m => m.Faculty , Model.Faculties )

MVC: Passing Data from controller to view [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How i can achieve passing data from controller to view without any action on view.
In my project ,
Event handler will raise an event in controller that will change the data of view in MVC.
I was searching for the answer and i come to know that
There should be some action on view but i dont need it.
EventHandler will raise an event and that will pass data to view
You obviously want to fire server events on the client, don`t you?
To achieve this you need to use SignalR. You can find a very good article here:
http://www.asp.net/signalr/overview/signalr-20/getting-started-with-signalr-20/tutorial-getting-started-with-signalr-20-and-mvc-5
In your controller:
public ActionResult getID()
{
string qry = "SELECT COUNT(JobID) FROM tblDriverJob";
IEnumerable<int> data = db.Database.SqlQuery<int>(qry);
int newFuelID = data.Single() + 1;
string i = Convert.ToString(newFuelID);
return new ContentResult { Content = i };
}
and then in your View
<div class="form-group ">
#Html.LabelFor(model => model.JobID, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-10">
#Html.EditorFor(model => model.JobID, new { htmlAttributes = new { #class = "form-control", #Value = #Html.Action("getID"), #readonly = "readonly" } })
#Html.ValidationMessageFor(model => model.JobID, "", new { #class = "text-danger" })
</div>
</div>
This will fill this with the results from my Query.
NOTE: This will return a single value (count+1 to get new jobID) from my query. This design can also be used (with a little 'jigging' for a Drop down list (where I would return a list to the view).
normally there are two ways for passing values from controller to view
1) you can store your values in ViewBag or in ViewData, this is preferable when your value is simple like a string, message,any id etc,
2) But if your data is of complex type like a search query result, then you should create a model with properties to store your multiple values, and then pass this model to the view while returning to view from controller's action (i.g. return (viewname, modelObject)), then you can access this model by defining the model at the top of your view like this #model yourmodelclassname, and then you can access each property of this model on your view by using the "Model" keyword.

ASP.Net MVC custom datatype passing additional values

I have created the custom "DataType" annotation to use on the model object to tell this is the date field on the view. ([DataType("Date")]) If I use #Html.EditorFor(model => model.DateCreated), it will act as a date field and pup up the JavaScript date picker. This is the template I am using under EditorTemplates
#inherits System.Web.Mvc.WebViewPage<DateTime>
<div class="input-append date" data-date="12-02-2012">
<input type="text" class="span2">
</div>
View -
<div class="control-group">
#Html.LabelFor(model => model.DateCreated, new { #class = "control-label" })
<div class="controls" id="date-container">
#Html.EditorFor(model => model.DateCreated, new { #class="input-append date"})
#Html.ValidationMessageFor(model => model.DateCreated, null, new { #class = "help-inline" })
</div>
</div>
Model -
[Display(Name = "Date Created")]
[DataType("Date")]
public DateTime DateCreated { get; set; }
Controller -
public ActionResult Create(int id)
{
// Attempt to get new customer object
GetPaymentResponse paymentResponse = _BillingService.GetPayment(id);
// Check whether response was successful
if (paymentResponse.State == BaseResponseState.Valid)
{
paymentResponse.Payment.Type.AvailableOptions = paymentResponse.Payment.Type.AvailableOptions.Where(x => x.Value != "BalancingTransaction").ToList();
ViewData.Model = paymentResponse.Payment;
return View();
}
}
I need to pass some additional value to my view via datatype from the model.
E.g. [DataType("Date"), Format("dd/mm/yy"), StartDate("12-02-2012")]
Could you please let me know how can I grab these additional value from the template? (I am new to ASP.Net MVC and I am using MVC 3)
Thanks
If you specify extra information using attributes, the information must be constant for all the instances of the class in which you define the member. I.e. the StartDate will be the same for all instances of your model, becasue the start date specified in the attribute must be a constant.
If that serves your purpoes, you can use a custom metadata provider to get specific metadata in your model from your custom attributes.
If you need to pass different data fro each case, you have to use any of the overloads of EditorFor which allows to pass extra view data. Then you can read that extra information from the ViewData in your template.
Be warned that there are some caveats in the metadata providers and custom template implementations, and registration. Take into account if your type can be made nullable, like DateTime?
If you use the Model Binding it should take the value properly I believe.
In your view, set the model on the first line, line this:
#model MyViewModel
and then in your controller, instead of passing the Model through the ViewData, do something like this:
var model = new MyViewModel();
// do stuff with your model here
return View(model);
Assuming that StartDate is a property of Payment
<div class="control-group">
#Html.LabelFor(model => model.StartDate, new { #class = "control-label" })
<div class="controls" id="date-container">
#Html.EditorFor(model => model.StartDate, new { #class="input-append date"})
#Html.ValidationMessageFor(model => model.StartDate, null, new { #class = "help-inline" })
</div>
</div>

Categories