Have Required Attribute apply to property in one view and not another? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
Say I have a class called Test and I have a Create and Edit view. Say this class is incredibly simple
public class Test
{
[Required]
public string str { get; set; }
}
Is it possible to remove the required attribute when a user is editing this object?

ViewModels are there for this. One for Create and one for Edit.
You should use a ViewModel for this, as you need View Specific Models here :
public class CreateTestViewModel
{
[Required]
public string str { get; set; }
}
and:
public class EditTestViewModel
{
public string str { get; set; }
}
You might want to read about What is ViewModel in MVC and How to use ViewModel in MVC

Related

C# how does this code work? [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 5 years ago.
Improve this question
Public class form1
{
List<Data> dataInfo = new List<Data>();
private void UpdateBinding()
{
supplierBox.DataSource = dataInfo;
supplierBox.DisplayMember = "Supplier";
}
}
I have another class called Data:
public class Data
{
public string Supplier { get; set; }
}
This is able to update my textBox named supplier on Visual Studio with data from my SQL Server that I have grabbed. How would I access the data from my SQL Server if I just wanted to put it in a variable like var?
supplier.Text = dataInfo.FirstOrDefault().Supplier;
would place the first element from your list of suppliers into your text box named "suppliers". If you don't have any items in your list of dataInfo then you would get a NullReferenceException.

Entity frame work .Cannot implicitly convert type 'object' to 'System.Data.Entity.DbSet<object>' [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 5 years ago.
Improve this question
i am trying to save the object it did not allow me to do so.
giving me type conversion error.
i have populate an object and trying to pass it to DBset, and save it use savechanges.
public class UserContext: DbContext
{
public UserContext()
{
}
public virtual DbSet<Users> User { get; set; }
// public virtual DbSet<Role> Roles { get; set; }
}
saving using this method
public bool registerUser(Users user)
{
userContext = new UserContext();
userContext.User = user;
userContext.SaveChanges();
return false;
}
Cannot implicitly convert type 'object' to 'System.Data.Entity.DbSet'
Need help!
I think, you need to call Add method:
public bool registerUser(Users user)
{
userContext = new UserContext();
userContext.User.Add(user);
userContext.SaveChanges();
return false;
}

List that contains list that contains list and so on in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
This is code:
public class Comment
{
public DateTime CreateAt { get; set; }
public Comment ParentComment { get; set;}
public List<Comment> SubComments { get; set; }
public string Text { get; set; }
}
I'am trying to do something like on facebook. You can comment every single comment, so there will be like a tree of comments.. I'am having trouble with trying to display all the comments, precisely text. I can't figure out how to do that. If anyone could help I'll appreciate it!
You can use recursion for process each comment and their sub comments for example:
public void checkComment(Comment comment)
{
//Check if the comment is valid
if (comment != null)
{
//Do whatever you want to do with your comment for example print to console
Console.WriteLine(String.Format("Comment: {0}", comment.Text));
//Check if i have any sub comments
if (comment.SubComments.Count > 0)
{
//Process each sub comment (recursive)
comment.SubComments.ForEach(x => checkComment(x));
}
}
}

Best way to store user's input in a multi-step application [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
My program consists of a number of steps. Say each step is in a tabpage on which upon completion, the user selects "Next".
What is the best/fastest way of storing these information?
I would generally say that this would be one Model to keep all the data - which then gets forwarded to the processing method once the user says "ok" at the last page. BUt then this depends a lot on actual usage scenarios, so that may not be the best way all the time.
I created a separate class that had properties for the 'answers' on the individual pages. Not sure if this is the best way to do it, but it worked perfectly for me. The user could go back and forth between the pages and the answers were stored. To Prescott's point, if they need to come back later perhaps you could save their partial form out as XML.
Create a viewmodel that represents the wizard, and in that viewmodel all the data related to your steps. You can also keep track of current step, and implement some methods to enable/disable next and previous buttons etc.
Your actual wizard window and tabs (the views) then all work with the wizard viewmodel (the data).
I would go with In-Memory Repository if you just want to retain this information for program execution, For example
namespace Repository {
public class InMemoryRepository : IRepository{
static readonly List<Item> items = new List<Item> {
new Item("CurrentTaxRate", 20m),
new Item("CurrentAmount", 100m)
};
public Item Get(string name) {
return items.Single(x => x.Name == name);
}
}
}
public class Item {
public Item(string name, decimal value) {
Name = name;
Value = value;
}
public string Name { get; private set; }
public decimal Value { get; private set; }
}
You can bolt on Add, Delete or Edit functionality easily to this and replace the "Item" here with your own "UserData" type.
Use dependency injection for getting this information back
public class TaxRate : ITaxRate {
private readonly IRepository repository;
public TaxRate(IRepository repository) {
this.repository = repository;
}
public decimal Get() {
var item = repository.Get("CurrentTaxRate");
return item.Value;
}
}

Display datagrid based on one filter in wpf [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 9 years ago.
Improve this question
I have datagrid and one value slider.I have three columns .Three columns like name,age and telephone number.Filter value is set to age.If i change slider value that is filter(age) based on filter DataGrid needs to display the data.I am using observable collection.
I think i understood what you want exactly.
Assuming that the name of your Data Grid is "MyDataGrid" and it's data source is bound to a class "MyDataGridItem" with this structure
public class MyDataGridItem
{
public string Name { get; set; }
public int Age { get; set; }
}
Subscribe the ValueChanged event of the slider and get the value and use LINQ where query to filter your results.
private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var sliderValue = (int) MySlider.Value;
MyDataGrid.ItemsSource = students.Where(item =>item.Age<sliderValue);
}

Categories

Resources