开发者

HttpPost Edit Action for updating my model

I try to create an asp.net mvc3 application.

Here's my view:

@model Iads.Elrams.Data.Entities.Page
@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <div class="formElement">
            <div class="formFieldLabel">
                @Html.LabelFor(model => model.ExternalLink)
            </div>
            <div class="formField">
                @Html.TextBoxFor(model => model开发者_运维知识库.ExternalLink, new { style = "width:400px" })
            </div>
        </div>
    <div class="border content">
        <div id="tabs" style="margin-top: 20px;"> <!-- jquery UI -->
            <div class="tabMenu"> 
                <ul>
                    @foreach (Iads.Elrams.Data.Entities.Language lang in ViewBag.Languages) {
                        <li><a href="#@lang.Iso2">@lang.Name</a></li>
                    }
                </ul>
            </div>
            @{
                foreach (Data.Entities.Language lang in ViewBag.AvailableLanguages) {
                    <div id="@lang.Iso2" class="border tabMenuContent">
                        <div class="formElement">
                            <div class="formFieldLabel">
                                @Html.LabelFor(model => model.GetTextOrDefaultByLang(lang.Iso2).Title) * <!-- Object: PageText -->
                            </div>
                            <div class="formField">
                                @Html.EditorFor(model => model.GetTextOrDefaultByLang(lang.Iso2).Title, new { style = "width:400px" })
                            </div>
                        </div>
                        <div class="formElement">
                            <div class="formFieldLabel">
                                @Html.LabelFor(model => model.GetTextOrDefaultByLang(lang.Iso2).NavText) *
                            </div>
                            <div class="formField">
                                @Html.EditorFor(model => model.GetTextOrDefaultByLang(lang.Iso2).NavText, new { style = "width:400px" })
                            </div>
                        </div>
                    </div>
                }
            }
        </div>
        <p>
            <input type="submit" value="save" class="button" />
            <input type="button" class="lightbutton" value="cancel" onclick="location.href = '@Url.RouteUrl(new RouteValueDictionary(new {
                    area = "Cms",
                    controller = "Page",
                    action = "Index"
                }))';" />
        </p>
    </div>
}

And here my page controller:


public ActionResult Edit(int id) {
    Page page = pageRepository.GetById(id);
    if(page == null)
        return new HttpNotFoundResult();

    IEnumerable languages = languageRepository.GetAll();
    languages = languages.OrderBy(m => m.LanguageId);
    ViewBag.AvailableLanguages = languages;
    return View(page);
}

[HttpPost]
public ActionResult Edit(what are the parameters???) {
    // ?????
    return View(page);
}

I would like to have the page as the parameter but this doesn't work with the PageTexts!!

What are my parameters? And how should my method look?

Any help would be appreciated.

thanks!


I think in your case you would make the parameter a Iads.Elrams.Data.Entities.Page. You could also make it a FormCollection and get at the request items that way.

Here's a couple of update type methods I've done:

<HttpPost()>
Function Transfer(ByVal collection As FormCollection)
...
End Function

<HttpPost()>
Function Update(ByVal visit As VisitSummaryViewModel)
...
End Function


You can use FormCollection and process the key-value pairs, but this is not an elegant solution. I would recommend the use of a separate view-model class, distinct from your Iads.Elrams.Data.Entities.Page, then you can use a library like AutoMapper to help you map the values. The view models and your database models can differ quite a lot, you can get greater flexibility by designing distinct classes (validation, custom logic etc).

In your case, I would a create a PageViewModel class which contains a collection of PageLanguageViewModel class. Also, in the view I would use the "for (i = 0; ... )" looping so that the html input extension methods would created appropriate names and ids for this collection. And your post parameter would be an instance of PageViewModel class. The ASP.NET Mvc model binding works for collection types also. However, there is a trick when removing or adding items dynamically in the view, because you have to maintain the correct index.


//
// GET: /controllername/Edit/
public ActionResult Edit()
{
    var langer = from lang in globalizationdb.langs select langs;
    ViewData["listlang"] = langer;
}

//
// POST: /controllername/Edit/
[HttpPost]
public ActionResult Edit(int id, FormCollection collection) {
                //dbcontext   //dbname            //id field
var languages = globalizationdb.lang.Where(x => x.lang_id == id).Single();

   long langish = Convert.ToInt64(collection["langfield"]);
   //get the lang id , im asuming u gonna use dropdownlist for lang...
   lang.lang_id = langish;

   //dbcontext 
   globalizationdb.SubmitChanges();
   return RedirectToAction("Index");
}

also at View ///controller name should be without blabla"Controller" at end like CatController should be Cat only..

@using (Html.BeginForm("Edit", "yourControllername", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="editor-label">Lang ID</div>
    <div class="editor-field">
        //lang field as dropdownlist
        @Html.DropDownList("langfield", new SelectList((System.Collections.IEnumerable)ViewData["listlang"], "lang_id", "lang_name",Model.lang))
        @Html.ValidationMessageFor(model => model.lang_id)
    </div>
}

after u use FormMethod.Post, new { enctype = "multipart/form-data" } on form start u can use FormCollection method.

u can use this also for ur own made db's..

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜