Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

razor: show editable list

// You need form tag in order for HttpPost. The trick is to either use for loop to generate
// correct Ids, or create Partial View.
// I like to use partial view, since it is a bit cleaner.

// For example

// ViewModel
public class SampleViewModel
{
    public IList<IncidentListmodel> IncidentListmodel = new List<IncidentListmodel>();
}


// Model
public class IncidentListmodel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Title { get; set; }
    public string Note { get; set; }
}


// View
@model AspNetMvc.Models.SampleViewModel

@foreach (var item in Model.IncidentListmodel)
{
    @Html.Partial("_UpdatePartial", item)
}


// Partial View (_UpdatePartial.cshtml)
@model AspNetMvc.Models.IncidentListmodel

@using (Html.BeginForm("Updateinc", "Home"))
{
    @Html.HiddenFor(m => m.Id)
    @Html.HiddenFor(m => m.Name)

    @Html.DisplayFor(m => m.Name)
    @Html.DisplayFor(m => m.Title)

    @Html.TextAreaFor(m => m.Note, 2, 20, new { maxlength = 50 })

    <button type="submit">Update</button>
}


// Controller
public class HomeController : Controller
{
    public ActionResult Updateinc()
    {
        var model = new SampleViewModel
        {
            IncidentListmodel = new List<IncidentListmodel>
            {
                new IncidentListmodel {Id = 1, Name = "One", Note = "Sample text"},
                new IncidentListmodel {Id = 2, Name = "Two"},
                new IncidentListmodel {Id = 3, Name = "Three"},
            }
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Updateinc(IncidentListmodel viewModel)
    {
        // Rediret to different page.
        return RedirectToAction("Index");
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: como guardar archivo en un botón asp.net 
Csharp :: c# an object on upper level cannot be added to an object 
Csharp :: cancellationtoken.linkedtokensource c# example 
Csharp :: Dominosteine c# 
Csharp :: git set origin url 
Html :: html dollar symbol 
Html :: html empty character 
Html :: ion-item remove bottom line 
Html :: htmjl favicons 
Html :: align eliment in center of row bootstrap 
Html :: flutter build web html renderer 
Html :: how to center html heading 
Html :: Add Random Image from Web in html 
Html :: Uncaught ReferenceError: jQuery is not defined 
Html :: slick slider cdn 
Html :: how to set video speed html 
Html :: input hidden 
Html :: bootstrap 5 text bold 
Html :: input number maxlength 
Html :: how to add a logo icon in HTML 
Html :: how to change font size in html 
Html :: link javascript to html 
Html :: html javascript input numbers only 
Html :: indian phone pattern regex 
Html :: html year input 
Html :: bold in bootstrap 
Html :: how to create white space in html 
Html :: laravel csrf fields 
Html :: fontawesome online link 
Html :: empty link javascript 
ADD CONTENT
Topic
Content
Source link
Name
7+9 =