Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I work in MVC,
The solution got divided into 3 projects:

1. I do not know Bidiok what it was for - CII.API
    A. It has a folder: App_Start,
    B. Veterans -Areas \ HelpPage which has a lot of folders: App_Start, Controllers, ModelDescriptions, Models, SampleGeneration, Views
    C. Controllers
    D. Mailers
    God. Models
    And. Views
    And all kinds of files ....
2. The project is used to link to the DB which is Models
3. The project which has all the MVC


I have a screen where I need to keep data: half of the screen is the view of the fixed content (phone, logo, etc.)
And half of the total content varies by language (name, address, etc. - any time should keep according to the language which is currently Rolodex).
I used 2 view and ajax to display the bottom - which is the save button, and the fixed part of contents also have a separate save ..
(I want to do everything by one save, but could not at the moment)


Save- button keeps data on this page and then returns to the screen of the index-
And it works fine when I'm running through the visual.

But when I bring up the server is not making the save - and returns all the site's landing page ...

The error it brings me to chrome:
(GET http://5.100.250.76/favicon.ico 500 (Internal Server Error
And in certain places: Index? Catid = 2 & cityId = 10: 971 GET http://5.100.250.76/CII.Web/Media/Get 404 (Not Found)

I compared the web.config are pretty much the same (except that everyone went to his own DB)

What else could it be?

Thank you!!

the view:
HTML
@model CII.Domain.POIs

@{
    ViewBag.Title = "Edit POI";
    int langId = ViewBag.LangId;
    long POIId = ViewBag.POIId;
    @Html.Hidden("LanguageId", langId)
    List<CII.Domain.Language> langlist = (List<CII.Domain.Language>)ViewData["languages"];
}
@using (Html.BeginForm("Edit", "POI", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <input type="hidden" value=@Model.CategoryId id="CategoryId" />
    <input type="hidden" value=@Model.POIs_Lang id="langId" />
   <div class="form-horizontal">
        <h4>POIs</h4>
        <hr />
        <input hidden="hidden" id="id" value=@Model.POIId />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.POIId)
        @Html.HiddenFor(model => model.LogoFileId)
        @Html.HiddenFor(model => model.ImageFileId)
 <div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
    <div class="container" style="tab-size:inherit">
        <ul class="nav nav-tabs">
            <li class="active"><a data-toggle="tab" href="#menu2" >English</a></li>
            <li><a data-toggle="tab" href="#menu2" >Hebrew</a></li>
            <li><a data-toggle="tab" href="#menu2" >French</a></li>
            <li><a data-toggle="tab" href="#menu3" >Russian</a></li>
            <li><a data-toggle="tab" href="#menu3" >Spanish</a></li>
            <li><a data-toggle="tab" href="#menu1" >German</a></li>
            <li><a data-toggle="tab" href="#menu2" >Italian</a></li>
            <li><a data-toggle="tab" href="#menu3" >Dutch</a></li>
            <li><a data-toggle="tab" href="#menu3" >Chinese</a></li>
            <li><a data-toggle="tab" href="#menu1" >Japanese</a></li>
            <li><a data-toggle="tab" href="#menu2" >Arabic</a></li>
        </ul>

    </div>
}

<div id="lang"></div>


<script type="text/javascript">

    function toEdidLang(langId) {
        debugger;
        var id = $("#id").val();
        //var city=$("")
        ///CII.Web
        var target = "/POI/EditLang/?id=" + id + "&langId=" + langId + "&POIId=" + POIId
        $.ajax({
            url: target,
            type: "GET",
            async: true,

            success: function (result) {
                $("#lang").html(result);
            },
        })
    }
</script>

the controller:
C#
public ActionResult Edit([Bind(Include = "POIId,CategoryId,WebsiteUrl,PhoneNumber,PhoneNumber_Spa,PhoneNumber_RoomServices,PhoneNumber_Concierge,Latitude,Longitude,LogoFileId,Location,FromDate,ToDate,CityId,ChainId,Recommended,ImageFileId,HotelId,OpenDay1,EndDay1,OpenDay2,EndDay2,OpenTime1,CloseTime1,OpenTime2,CloseTime2")] POIs pois,
    ICollection<POIs_Lang> poilang,
    HttpPostedFileBase[] imgfiles, string[] imageids,
    int[] keep)
{
    if (ModelState.IsValid)
    {
        if (pois.POIId == 0)
        {
            db.POIs.Add(pois);
        }
        //save tags
        else
        {
            db.Entry(pois).State = EntityState.Modified;
            //using (check_in_israelEntities db1 = new check_in_israelEntities())
            //{
            //    var pp = db1.POIs.Include(s => s.Tags).Where(p => p.POIId == pois.POIId).First();
            //    pp.Tags.Clear();
            //    db1.SaveChanges();
            //}

        }
        //if (tagsList != null)
        //{
        //    foreach (var tagid in tagsList)
        //    {
        //        pois.Tags.Add(db.Tags.Find(tagid));
        //    }
        //}

        //save/remove images
        POIsImage img;
        if (keep!=null)
        {
            Guid[] removedimgs = new Guid[keep.Count()];
            int r = 0;
            for (var x = 0; x < keep.Length; x++)
            {
                if (keep[x]==0)    //remove
                {
                    long id;
                    if (long.TryParse(imageids[x],out id))
                    {
                        img = db.POIsImages.Find(id);
                        if (img!=null)
                        {
                            db.POIsImages.Remove(img);
                            removedimgs[r++] = img.FileId;

                            db.SaveChanges();
                        }
                    }
                }
            }
            r = 0;

            //הערה שלי: הוספת הרבה תמונות לנקודת עינין
            for (var x = 0; x < keep.Length; x++)
            {
                if (keep[x] == 1)               //save
                    if (imgfiles.First() != null)
                    {
                        var file = imgfiles.Where(p => p.FileName == imageids[x]).FirstOrDefault();
                        if (file != null && file.ContentLength > 0)
                        {
                            img = new POIsImage();
                            img.POIId = pois.POIId;
                            img.DisplayOrder = x;
                            //if (img.FileId == null)
                            img.File = Domain.MediaServices.Save(removedimgs[r++], file, db);
                            //else
                            //    img.File = Domain.MediaServices.Save(img.FileId, file, db);
                            db.POIsImages.Add(img);
                            db.SaveChanges();
                        }
                    }
            }
        }

        //save image
        //if (Request.Files["logo"] != null && Request.Files["logo"].ContentLength > 0)
          //  pois.File = Domain.MediaServices.Save(pois.LogoFileId, Request.Files["logo"], db);
        //if (Request.Files["image"] != null && Request.Files["image"].ContentLength > 0)
        //    pois.File1 = Domain.MediaServices.Save(pois.ImageFileId, Request.Files["image"], db);

        //save language
        foreach (var cl in poilang)
        {
            bool exists = db.POIs_Lang.Where(d => d.POIId == pois.POIId && d.LanguageId == cl.LanguageId).Any();
            cl.POIId = pois.POIId;
            if (!exists)
            {
                if (cl.Name != null)
                    db.POIs_Lang.Add(cl);
            }
            else
                db.Entry(cl).State = EntityState.Modified;
        }
        db.SaveChanges();
        pois.Location = LocationUtils.CreateLocation(pois.Latitude, pois.Longitude);



        return RedirectToAction("Index", new { catid = pois.CategoryId });
    }

    ViewBag.CategoryId = new SelectList(db.Categories.Select(c => new { CategoryId = c.CategoryId, Name = c.Categories_lang.FirstOrDefault().CategoryName }), "CategoryId", "Name", pois.CategoryId);
    //ViewBag.CityId = new SelectList(db.Cities.Select(c => new { CityId = c.CityId, Name = c.Cities_lang.FirstOrDefault().CityName }), "CityId", "Name", pois.CityId);
    //ViewData["languages"] = db.Languages.OrderBy(l => l.LanguageId).ToList();
    return View(pois);
}

// GET: /POI/Create
public ActionResult EditLang(long id, int langId)
{
    POIs_Lang result = db.POIs_Lang.Where(d => d.POIId == id && d.LanguageId == langId).Include(s => s.POIs).FirstOrDefault();
    if (result == null)
    {
        result = db.POIs_Lang.Create();
        result.POIId = id;
        result.POIs = db.POIs.Find(id);
        result.LanguageId = langId;
        result.Language = db.Languages.Where(l => l.LanguageId == langId).First();
    }

    return View(result);
}

// POST: /POI/edit
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
public ActionResult EditLang(POIs_Lang pois, long POIId)
{
    POIs data = db.POIs.Find(pois.POIId);
    if (ModelState.IsValid)
    {
        bool exists = db.POIs_Lang.Where(d => d.POIId == POIId && d.LanguageId == pois.LanguageId).Any();

        if (!exists)
            db.POIs_Lang.Add(pois);
        else
            db.Entry(pois).State = EntityState.Modified;

        if (pois.Name == null)
            pois.Name = "";
        if (pois.Address == null)
            pois.Address = "";

        //save image
        if (Request.Files["attachment"] != null && Request.Files["attachment"].ContentLength > 0)
            pois.File = Domain.MediaServices.Save(pois.AttachementId, Request.Files["attachment"], db);

        if (Request.Files["coupon"] != null && Request.Files["coupon"].ContentLength > 0)
            pois.File1 = Domain.MediaServices.Save(pois.CouponAttachementId, Request.Files["coupon"], db);


        db.SaveChanges();

        //long? id, int catid, int langId

        //return View("Edit", langId = db.POIs_Lang.Find(pois.POIId).LanguageId);
        return RedirectToAction("Edit", new { id = POIId, catid = data.CategoryId, langId = pois.LanguageId });
    }

    return View(data);
}

the view "poi_lang"
HTML
@model CII.Domain.POIs_Lang

@{
    ViewBag.Title = "Edit POI localized data";
}


@using (Html.BeginForm("EditLang", "POI", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.POIId)
        @Html.HiddenFor(model => model.LanguageId)
        @Html.HiddenFor(model => model.AttachementId)
        @Html.HiddenFor(model => model.CouponAttachementId)
    </div>
    <table style="width:100%;">
        <tr>
           
            <td>
                @if (Model != null && Model.AttachementId.HasValue)
                {
                    <div class="form-group">


                        <label class="control-label col-md-2">Attachment</label>
                        <a class="col-md-10" href="@Url.RouteUrl("Media", new {id=Model.AttachementId})" target="_blank">View Attachment</a>
                        <a class="col-md-10" href="#" >Delete Attachment</a>




                    </div> }
                <div class="form-group">
                    <label class="control-label col-md-2">Upload a new Attachment </label>
                    <div class="col-md-10">
                        <input type="file" id="attachment" class="form-control" name="attachment" />
                        @if (Model != null && Model.AttachementId.HasValue)
                        {
                            <p class="help-block">The old attachment will be replaced !</p>
                        }
                    </div>
                </div>


                @if (Model != null && Model.CouponAttachementId.HasValue)
                {
                    <div class="form-group">


                        <label class="control-label col-md-2">Coupon</label>
                        <a class="col-md-10" href="@Url.RouteUrl("Media", new {id=Model.CouponAttachementId})" target="_blank">View Coupon</a>
                        <a class="col-md-10" href="#" >Delete Coupon</a>




                    </div> }
                <div class="form-group">
                    <label class="control-label col-md-2">Upload a new Coupon </label>
                    <div class="col-md-10">
                        <input type="file" id="coupon" class="form-control" name="coupon" />
                        @if (Model != null && Model.CouponAttachementId.HasValue)
                        {
                            <p class="help-block">The old coupon will be replaced !</p>
                        }
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Save" class="btn btn-default" />
                    </div>
                </div>

            </td>
        </tr>
    </table>
}

@*<div>
    @Html.ActionLink("Back to POI List", "Index", new { catid = Model.POIs.CategoryId })
</div>*@
Posted

1 solution

Hello,
Here the problem is with the routing for which the domain and server map path do not match. In local when you try out, the localhost/ can manage and redirect you to proper route. You need to set the path correctly in the javascript where you make an ajax call.
The problem is here:
var target = "/POI/EditLang/?id=" + id + "&langId=" + langId + "&POIId=" + POIId

C#
var splitUrl = location.href.split(location.hostname);
   splitUrl = splitUrl[1].split('/');

Please use like :
var target = "/"+splitUrl+"/POI/EditLang/?id=" + id + "&amp;langId=" + langId + "&amp;POIId=" + POIId


Hope this helps and works.
 
Share this answer
 
Comments
Member 11919288 7-Sep-15 5:20am    
For use in one SAVE - how you can use 2model:
@model CII.Domain.POIs
And @ model CII.Domain.POIs_Lang???

Thank you very much!!
Passion4Code 7-Sep-15 5:57am    
hello,
For that you need to define a model inside a main model. Just a hierarchy like Parent Child. Suppose,
One model is like
Automobile(){
public CarModel car {get;set;}
}
public CarModel{
public string CarName{get; set;}
public string CarCompany{ get;set;}
}
Then use @model ---.Automobile in the razor view page.
If helps please accept the answer.
Thanks
Member 11919288 7-Sep-15 8:39am    
Where to spell it?
Member 11919288 7-Sep-15 8:40am    
And how to apply it in VIEW?
Passion4Code 7-Sep-15 8:41am    
At the very top of your cshtml page

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900