开发者

How can I safely access key groups in the FormCollection?

开发者 https://www.devze.com 2023-04-11 20:17 出处:网络
I have a table where each tr is grouped by having their input elements name set to a value that is unique for each row.

I have a table where each tr is grouped by having their input elements name set to a value that is unique for each row.

For example,

    <td>
        <input data-field="opps" type="text" value="somevalue" name="@item.Code" />
    </td>
    <td>
        <input data-field="asc" type="text" value="somevalue2" name="@item.Code" />
    </td>

On POST

[HttpPost]
        public ActionResult Update(FormCollection collection)
        {
           开发者_StackOverflow中文版 try
            {
                //doin work on collection...order assumed static

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

my System.Web.MVC.FormCollection is grouped in the same order I define the <td>. I don't like to assume order, but without access to my data-field, I'm not sure what else I can do (maybe I could append the data-field value as a prefix to the name and put it all together with a custom collection and Regex..but that seems nutty).

Is there a way to access the data-field? This way I'm not fragile to re-ordering or adding new columns in the View.


Let's say you have a class (model) defined like this:

public class MyModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

In you controller, you might have an action called Create, like so:

[HttpGet]
public ViewResult Create()
{
    MyModel sampleModel = new MyModel();
    return View(sampleModel);
}

[HttpPost]
public ActionResult Create(MyModel sampleModel)
{
    if (ModelState.IsValid)
    {
        TempData["Error"] = "There were errors. Please correct the problem and submit again";
        return View(sampleModel);
    }

    // At this point everything is fine and you can access data in your sampleModel
    if (sampleModel.Age >= 16)
    {
        return RedirectToAction("LegalAccess");
    }
    else
    {
        TempData["Error"] = "You must be 16 or over to access this site";      
        return RedirectToAction("AgeRestriction");
    }
}

When you create a strongly typed view that uses MyModel as model you might define it something like this:

@model MyModel

@{
    Layout = "~/Shared/_Layout.cshtml";
}

@using (Html.BeginForm()) 
{
    @Html.LabelFor(m => m.FirstName)
    @Html.TextBoxFor(m => m.FirstName)
    <br />
    @Html.LabelFor(m => m.LastName)
    @Html.TextBoxFor(m => m.LastName)
    <br />
    @Html.LabelFor(m => m.LastName)
    @Html.TextBoxFor(m => m.LastName)

    <input type="submit" value="Submit" />        
}

When you submit this form, the model binder will in the background copy data from this form using Request.Form into an object of type MyModel which it creates in the background. This new object is passed to an action that handles HTTP POST method. With this you get strongly typed object and you don't have to worry about the order of items in FormCollection.

I hope I helped answer your question.

BTW. I wrote this without Visual Studio, so I hope there are not errors. :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号