logo
down
shadow

MVC - ICollection within List won't return to Controller on POST


MVC - ICollection within List won't return to Controller on POST

By : Daud Alzayer
Date : November 22 2020, 09:00 AM
To fix the issue you can do In my edit screens I can't seem to get an ICollection within a Model to return to the Controller on POST. For simplicity, there are 2 models, a Pattern table: , You need to index with a for loop instead of a foreach:
code :
     @for(var i = 0; i < Model.patterns[s].PatternFeatureAssociations; i++)
        { 
            @Html.HiddenFor(modelItem => Model.patterns[s].PatternFeatureAssociations[i].PkID)
            @Html.HiddenFor(modelItem => Model.patterns[s].PatternFeatureAssociations[i].PatternID)        
            <h3>Feature Name</h3>
            <div>@Html.EditorFor(modelItem => Model.pattern    s[s].PatternFeatureAssociations[i].FeatureName)</div>    
            <h3>Feature Description</h3>
            <div>@Html.EditorFor(modelItem => Model.patterns[s].PatternFeatureAssociations[i].FeatureDesc)</div>               
    }


Share : facebook icon twitter icon
List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?

List, IList, IEnumerable, IQueryable, ICollection, which is most flexible return type?


By : Mihai Coman
Date : March 29 2020, 07:55 AM
I hope this helps you . Collections are not generally very useful for DAL returns, because a collection does not implicitly guarantee order. It's just a bucket of items. An IList, on the other hand, does implicitly guarantee order. So we're down to IEnumerable or IList. The next question would be: is the List object "live"? i.e., is it connected to the data backing so that when you add an item to the IList, it will be reflected in the DB? For LINQ-to-SQL, this is not the case. Rather, you're supposed to attach entities to the tables. So unless you supply this additional wiring, a List is superfluous. Stick with IEnumerable.
OK to return an internal List<T> as an IEnumerable<T> or ICollection<T>?

OK to return an internal List<T> as an IEnumerable<T> or ICollection<T>?


By : Luke
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Not only there's nothing wrong with it, but it's actually good practice: expose only what is strictly necessary. That way, the caller can't rely on the fact that the method will return a List, so if for some reason you need to change the implementation to return something else, you won't break your contract. However the calling code might break if it (incorrectly) made assumptions about what the method actually returns.
What is the advantage of return ICollection over List

What is the advantage of return ICollection over List


By : user52979
Date : March 29 2020, 07:55 AM
I wish this help you IEnumerable provides access to a forward only cursor over a series of T objects ICollection provides the same as IEnumerable but also a Count property (meaning the collection has a definitive end) IList provides the same as ICollection but also random access to any element within the list via an indexer (list[5])
List implements all of the above.
How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?

How do I get an item from an icollection via index when in the class contrusctor I set the icollection as a list?


By : Serg Shulyak
Date : March 29 2020, 07:55 AM
To fix the issue you can do There's no need to try to access MsgHistory.Messages as though it was a list. The iterator variable in a foreach-loop is the current item in the list that you're looping through. So you don't need to get that item from the list. Try this:
code :
foreach (var msg in MsgHistory.Messages)
{
    MsgStr += msg.UserFrom + ": " + msg.Message + "<br>";
}
public ActionResult Details()//string userTo, string userFrom)
{
    var MsgHistory = PrivateMessageRepository.GetMessageHistoryByID(userTo, userFrom);
    var viewModel = MsgHistory.Messages.ToList();
    return View(viewModel);   
}
@model IEnumerable<DatabaseDesign.Models.PrivateMessageDetail>
...
@foreach(var msg in Model) {
    @:@msg.UserFrom: @msg.Messages<br>
}
public ActionResult Details()//string userTo, string userFrom)
{
    var MsgHistory = PrivateMessageRepository.GetMessageHistoryByID(userTo, userFrom);
    var viewModel = MsgHistory.Messages.Select(x => new { x.UserFrom, x.Message }).ToList();
    return View(viewModel);   
}
@model IEnumerable<dynamic>
...
@foreach(var msg in Model) {
    @:@msg.UserFrom: @msg.Messages<br>
}
jQuery post wont post data to ASP.NET API Controller

jQuery post wont post data to ASP.NET API Controller


By : Fredrik Sævland
Date : March 29 2020, 07:55 AM
To fix this issue POSTing a straight-up JSON array to an MVC controller doesn't work. The solution is to use the long-form AJAX jQuery method, include the traditional:true option, and structure your JSON data and model differently - see this answer.
Re the model: the parameter to your controller action should not be an array - it should be a model class which contains and array member - and of course your JSON should mirror this structure.
Related Posts Related Posts :
  • Why do I get a COMException when I try to initialize an Interop.Word.Application?
  • Finding subtotal for column in repeater control
  • dropdownlist item selected throw exception (stack empty)
  • How can I list IIS working processes programmatically?
  • How to access nested object from JSON with Json.NET in C#
  • OpenXml DataValidation set predefined List for columns
  • How to find recurring word groups in text with C#?
  • layout conrols to developp universal apps in windows 8.1 pro
  • Adding strings to a list via for loop xna
  • Using Vertex Arrays with OpenTK
  • Optimizing List<string>
  • C# Local variables
  • How can I merge the results of a group by Linq-to-XML query?
  • How to add Report in ASP.NET Project
  • C# How to display all instances of a loop in a label?
  • C# subscribe a function to System.Action that is subscribed to another System.Action: why does order matter?
  • Is there any negative effect to setting SqlCommand's CommandTimeout to a high value?
  • C# - TreeView Context Menu Out of Place when using keyboard
  • How to read stored procedure output and return it as list
  • How do you maintain changes made by VisualState triggers after setting another VisualState?
  • Should I be unit testing my bootstrapper and if so how?
  • using .SingleOrDefault() inside .Where() will raise the following exception:- System.NotSupportedException was unhandled
  • C# How would I fill a Form1 Parameter in the same seperate class
  • Access gridview row's data from LinkButton inside that row
  • Translating excel function to c# Math.pow not resulting in same values
  • Arranging Ten Inputted Numbers into Ascending and Descending order
  • AsEnumerable and Query Syntax
  • How can I defensively code against randomly referencing "Table 0" and null values?
  • Changing xaml code position based on screen size
  • Castle Windsor resolve ienumerable and name
  • Drawing multiple sprites with a for loop xna 4.0
  • For loop to Populate Textblocks
  • Visual Studio 2015: Create an Empty Project
  • What are .NET classes to replace the old MSXML2.ServerXMLHTTP?
  • Handle leak in .Net threads
  • Excel add-in ribbon click events bubbling
  • validation rule needed for only numbers to be entered
  • Parsing complex XML, no descendent elements exists when selecting a named element
  • Web Api - IEnumerable with complex type as param is null
  • Email address input validation
  • .Net AND operator Regular Expression using strings in a document
  • Windows phone 8.1 POST x-www-form-urlencoded not working
  • Is there any way to make my C# XMLManager work?
  • ASP.NET 5 Console Application (package) - How to create DBContext from connection string?
  • How to use form's function in other class C#
  • WPF Zebra EPL2 Printing issue
  • C# use reflection to capture exception throw
  • Convert strings in DataRow to double
  • Xml-Serialization is not working
  • HTML Agility Pack can't find classes with trailing spaces
  • ComputeHash Calls Inexplicably Differ
  • Creating an array that holds both integers and datetime C#
  • GroupBy multiple columns in Linq with Take()
  • Rename multiple symbols at once? (using Roslyn)
  • Cast lambda in delegate
  • DataContractSerializer using default properties
  • LINQ Query Sum in recordset
  • change left-hand boolean value to be opposite of the right hand value
  • Get node from XmlDocument using xpath
  • Why does a Control's BeginInvoke() target delegate never occur after Dispose() is called on the Control?
  • shadow
    Privacy Policy - Terms - Contact Us © animezone.co