Im beginer for the MVC , Im develop the MVC project i have a some dropdown menu, i want to remove client slide validation how can i do it. i was removed but not work
<div class="col-md-3">
#Html.DropDownListFor(a => a.Ach, new SelectList(ViewBag.AtList4, "AtId", "AName"), " Select a A", new { Class = "form-control dd", title = "aa", style = "width:175px;height:30px; margin-top:6px;font-size:small;" })
#Html.ValidationMessageFor(a => a.Ach)
</div>
model
[Key]
public int ItemTemplateId { get; set; }
[ForeignKey("MainGroup")]
public int MainGroupId { get; set; }
public virtual MainGroup MainGroup { get; set; }
[Required(ErrorMessage = "Required")]
[ForeignKey("SubGruop")]
public int SubGruopId { get; set; }
public virtual SubGroup SubGruop { get; set; }
public int Ach { get; set; }
Change the property Ach to a nullable int and then no value will be acceptable:
public int? Ach { get; set; }
I anticipate that setting this value to nullable isn't acceptable, because you don't want null values for the property within your database. Keep reading...
I notice that your View Model has [Key], [ForeignKey] and virtual properties, which suggests to me that you're using your domain models as view models.
You should really have a whole new class that has a specific use as a View Model for your page that contains the minimum number of properties to hold only the values that are required to display the view.
Related
I am rather new to Razor and I'm using the default templates. I have a ViewModel which contains three models:
public class CarAndOwnerViewModel
{
public IEnumerable<SelectListItem> Cars { get; set; }
public SelectListItem SelectedCar { get; set; }
public OwnerModel Person{ get; set; }
}
I am using this ViewModel in my create method. To get the list of Cars I use:
#Html.DropDownListFor(Model => Model.SeletedCar, new SelectList(Model.Cars, "Value", "Text", Model.Cars))
When I submit this drop down list the value of CarAndOwnerViewModel.SelectedCar is null. I Have a textbox that contains the following and it has it in CarAndOwnerViewModel.Owner.Name:
<input asp-for="Owner.Naame" class="form-control" />
What I filled out in that input box is passed on. I am not sure why the Input Boxes are working but the Select box isn't.
The issue was that the SelectedCar should be an int instead of a SelectedItem
Changing the class to
public class CarAndOwnerViewModel
{
public IEnumerable<SelectListItem> Cars { get; set; }
public int SelectedCar { get; set; }
public OwnerModel Person{ get; set; }
}
Fixed the issue.
I have a table for cards with a lot of columns. One of those columns I have named "LinkedTo". In my view I want to be able to have a button that says "Flip Over" which will show the card information on the back of the card (as it would be in real life). The reason I don't want to put the information for the front and back in the same record is for search capabilities and the way I want the details view to appear. In my edit view, I would like to show a dropdown list that displays only cards with the "Flip" column marked as true (to make the list much shorter). On save, the CardID from that dropdown list would be saved to the "LinkedTo" column. Then I would want to be able to call that CardID in a link/button on the details view as mentioned above. As it stands, anyway I try to create a dropdown list I get ambiguity errors or nothing shows. If my model already knows all cards, how do I make it show a filtered list of the cards when editing one specific card? Is there an easier way to do this?
I tried this in my controller:
ViewBag.Card_ID = new SelectList(db.Cards.Where(w => w.Flip == true), "CardID", "Title");
And this in my view:
#Html.DropDownListFor(model => model.Cards.CardID, ViewBag.CardID as SelectList)
This is my Edit ViewModel:
public class EditCardViewModel
{
public Card Cards { get; set; }
public HttpPostedFileBase ImageUpload { get; set; }
public int[] LinkedCard { get; set; }
public IEnumerable<SelectListItem> Abilities { get; set; }
public int[] SelectedAbilities { get; set; }
public IEnumerable<SelectListItem> Rarities { get; set; }
public int SelectedRarities { get; set; }
public IEnumerable<SelectListItem> MainTypes { get; set; }
public int SelectedMainTypes { get; set; }
public IEnumerable<SelectListItem> SubTypes { get; set; }
public int SelectedSubTypes { get; set; }
public IEnumerable<SelectList> CardSets { get; set; }
public int SelectedCardSets { get; set; }
public Rarity Rarity { get; set; }
public MainType MainType { get; set; }
public SubType SubType { get; set; }
public CardSet CardSet { get; set; }
public EditCardViewModel() { }
public EditCardViewModel(Card card)
{
Cards = card;
}
}
Any assistance would be very appreciated. :)
On POST the DropDownListFor section will bind the result to the field you tell it to. Right now you have that set to model.Cards.CardId. If you want that selection to be bound to the "LinkedTo" property then change it to that instead. Also I notice that you are passing in "ViewBag.CardId" instead of "ViewBag.Card_ID" that you show as the variable being set with the list.
#Html.DropDownListFor(model => model.Cards.LinkedTo, ViewBag.Card_ID as SelectList)
Here is a demo
I have a asp mvc 5 project using entity framework code-first. I created the controllers and views with scaffolding. Although i have not specified in the model that the Due date be [Required] and the item in the table has a value of NOT NULL.
Yet I am still getting this message on the POST
The DueDate field is required.
The model is really basic
public class Project
{
public int ProjectId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime DueDate { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}
}
This is a Datetime2 datatype using EditorFor
<div class="input-group">
#Html.EditorFor(model => model.DueDate, new { #class = "form-control datepicker" })
<div class="input-group-addon">
<i class="entypo-calendar"></i>
</div>
</div>
It is also using jquery bootstrap datepicker, Although i doubt these has any effect on this functionality. Really confused how this field is a required field yet it has not been set as such in any area of the solution or database.
That's normal. DateTime is a value type meaning that it will always require a value. The model metadeata provider in ASP.NET MVC automatically adds the required attribute to non-nullable data types. You could use a nullable DateTime:
public class Project
{
public int ProjectId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public DateTime StartDate { get; set; }
public DateTime? DueDate { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}
I am having difficulty with my understanding of MVC coming from an aspx world.
I have a Model called CustomerGarment. This has a Order and a Customer along with a few garments.
public class CustomerGarment
{
public int CustomerGarmentId { get; set; }
public virtual Customer Customer { get; set; }
public virtual Order Order { get; set; }
public virtual GarmentJacket GarmentJacket { get; set; }
public virtual GarmentShirt GarmentShirt { get; set; }
}
I have a method for get and post. When the page loads, it creates a new CustomerGarment instance and querys the database to fill the Customer and Order variables. I then use the viewbag to show on the screen a list of GarmentJackets and GarmentShirts
The page then views and using the view I can access the model perfectly. Drop downs load with the viewbag contents and I can access all Customer and Order variables using the model I have passed.
The problem I then face is when I use the HttpPost. The model is not passed back with the information I passed to it.
public ActionResult AddGarments(int orderId, int customerId)
{
CustomerGarment cg = new CustomerGarment();
cg.Order = (from a in db.Orders where a.OrderId == orderId select a).FirstOrDefault();
cg.Customer = (from a in db.Customers where a.CustomerId == customerId select a).FirstOrDefault();
var jackets = from a in db.GarmentJackets orderby a.Type, a.SleeveLengthInches, a.ChestSizeInches select a;
var shirts= from a in db.GarmentKilts orderby a.PrimarySize, a.DropLength select a;
ViewBag.GarmentJacket = new SelectList(jackets, "GarmentJacketId", "GarmentJacketId");
ViewBag.GarmentShirt = new SelectList(shirts, "GarmentShirtId", "GarmentShirtId");
return View(cg);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddGarments(CustomerGarment cg)
{
// Here, I do not have the customer info for example
db.CustomerGarments.Add(cg);
db.SaveChanges();
return RedirectToAction("Index");
return View(cg);
}
This is a bit of my view
#Html.HiddenFor(model => model.Order.OrderId)
#Html.HiddenFor(model => model.Order.CustomerId)
<div class="display-field">
#Html.DisplayFor(model => model.Customer.Name)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.GarmentJacket, "Jacket")
</div>
<div class="editor-field">
#Html.DropDownListFor(m => m.GarmentJacket, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"})
</div>
EDIT
My Garment Jacket Model
public class GarmentJacket : Garment
{
public int GarmentJacketId { get; set; }
[Required]
public string Type { get; set; }
[Required]
[Display(Name = "Chest Size")]
public int ChestSizeInches { get; set; }
[Required]
[Display(Name = "Sleeve Length")]
public int SleeveLengthInches { get; set; }
}
public class Garment
{
[DataType(DataType.Date)]
public DateTime? DateRetired { get; set; }
[Required]
public string Barcode { get; set; }
[Required]
public bool Adults { get; set; }
}
In your CustomerGarment class, you should have:
public class CustomerGarment
{
public int CustomerGarmentId { get; set; }
public int CustomerId { get; set; }
public int OrderId { get; set; }
public int GarmentJacketId { get; set; }
public int GarmentShirtId { get; set; }
public virtual Customer Customer { get; set; }
public virtual Order Order { get; set; }
public virtual GarmentJacket GarmentJacket { get; set; }
public virtual GarmentShirt GarmentShirt { get; set; }
}
And, then, in your View, your DropDownList will look like:
#Html.DropDownListFor(m => m.GarmentJacketId, (SelectList)ViewBag.GarmentJacket, new {style="width:312px;height:30px;margin-top:2px;margin-bottom:5px"})
Your DropDownList only posts one value, which is the GarmentJacketId. You can't bind that Id to the whole GarmentJacket class.
By the way, you also need to replace your hidden inputs with these:
#Html.HiddenFor(model => model.OrderId)
#Html.HiddenFor(model => model.CustomerId)
I think I know your problem. As you suggested in you comment above you need to post everything you want retained in the view. This is one of the differences beteween webforms and MVC, webforms has viewstate that could contain information that you don't explicitly add to the view and post back, giving the impression of state. In MVC you have to add it to the view.
On the other hand you don't need to pass in more information than you need either. You pass inn the customerId as a hidden field. On post method you get the customer from the db using the Id, then you add the order to the customer.
I have some questions about your design, but given that a customer holds a collection of Orders, you could do something like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddGarments(CustomerGarment cg)
{
// Get the customer from the database
var customer = db.Customers.Find(c=>c.id==cb.Customer.Id)
var order = new Order();
//Create your order here using information from CustomerGarment model
//If the model already holds a valid Order object then just add it.
//i.e. you could get a Garment object from the DB using the GarmentId from
//the ViewModel if you really need more than just the Id to create the order
customer.Orders.Add(order);
db.SaveChanges();
return RedirectToAction("Index");
}
I am new to asp.net mvc4 and creating a project in it. I am stuck on the way.
First let me describe about my project. I am creating a product table and each product has some attributes. I have created a ProductAttribute Model and a AttributeValue Model to add attributes and their values. Then I have created a ProductAttributeValue Model to assign the attributes and their values to the Product.
Now my problem is that I want to use the same view to add Attribute and their values. Below are my models:
[Table("ProductAttribute")]
public class ProductAttribute
{
[Key]
[MaxLength(20)]
public string AttributeId { get; set; }
[Required]
[Display(Name = "Attribute Name")]
[MaxLength(100)]
public string AttributeName { get; set; }
[Required]
[Display(Name = "Datatype")]
[MaxLength(50)]
public string AttributeDatatype { get; set; }
[Required]
[Display(Name = "Is Active")]
public bool IsActive { get; set; }
[Required]
[Display(Name = "Attribute Type")]
[MaxLength(30)]
public string AttributeType { get; set; }
}
[Table("AttributeValue")]
public class AttributeValue
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Required]
[MaxLength(20)]
public string AttributeId { get; set; }
[Required]
[MaxLength(100)]
public string AttributeName { get; set; }
[Required]
[MaxLength(200)]
public string AttributeVal { get; set; }
public virtual ProductAttribute ProductAttribute { get; set; }
}
How can I insert values to different tables using one view and controller? If there is another way to do the same then please help me about that.
Thanks
I think I understand what you are getting at. First you are going to want to build a View Model. Something like
public Class Tables
{
public List<ProductAttribute> Products { get; set; }
public List<AttributeValue> Values { get; set; }
}
Set those lists from your controller and pass them to the view. On the view you will define the model like
#model Tables
and then in the view build your table in your manner of preference. I have used Html.WebGrid in the past but have been using just foreach loops recently
<table>
foreach(var temp in Model.Products)
{
<tr>
<td>
temp.Name, etc
</td>
</tr>
}
</table>
as for adding and creating I have never been a fan of creating or adding directly in the table and generally what I am showing in the table isn't all of the information that I want from them so I use context menus or edit buttons on the rows and then a add button in a context menu or just outside of the table that will redirect to an edit/add page and then navigate back. Then you can refresh the table with a post back, jquery, refresh a partial view, whatever way fits your situation best. Hopefully this helps :)
Oh, here is a link to people discussing best how to edit a table
How to edit tabular data (ASP MVC)
Update:
Any information passed to the view through the model and used on the page with a for (html.textboxfor, textareafor, etc) will be passed back to the controller. If those fields are changed then the changed value will be passed back.
public ActionResult Index(){
(build your table class)
return View(Tables);
}
[HttpPost]
public ActionResult Index(Tables tbl){
(pass values return from the view to your query for saving to the database)
{